當我使用下面的查詢沒有限制嵌套子查詢限制子查詢
SELECT `c`.*,
GROUP_CONCAT(g.photo SEPARATOR "|") AS `photos_list`
FROM `contests` AS `c`
LEFT JOIN
(
SELECT `gallery`.`contest_id`,
`gallery`.`photo`
FROM `gallery`
) AS `g` ON c.id = g.contest_id
GROUP BY `c`.`id`
一切工作正常
id title photos_list
1 title1 50026c35632eb.jpg
2 title2 50026ac53567f.jpg|50026ac5ec82e.jpg|500e71557270f....
包子當我添加LIMIT,我只有一個行獲得「photos_list」 。下面的查詢
SELECT `c`.*,
GROUP_CONCAT(g.photo SEPARATOR "|") AS `photos_list`
FROM `contests` AS `c`
LEFT JOIN
(
SELECT `gallery`.`contest_id`,
`gallery`.`photo`
FROM `gallery`
LIMIT 0, 2
) AS `g` ON c.id = g.contest_id
GROUP BY `c`.`id`
將返回
id title photos_list
1 title1 NULL
2 title2 50026ac46ea05.jpg|50026ac53567f.jpg
項目與ID = 1必須包含photos_list
,但事實並非如此。值得注意的是,LIMIT確實爲id = 2。
我該怎麼做才能得到正確的結果?
所以你想限制photos_list級聯到2的照片數量? – dnagirl 2012-07-24 12:45:42
'LIMIT'適用於整個查詢。您只需從'gallery'中將兩行總計的行與您的查詢結果相關聯,這兩行恰好與'id = 2'關聯。 – mellamokb 2012-07-24 12:45:52
@dnagirl,是的,我正在嘗試限制照片。 – serg66 2012-07-24 12:52:24