我有两张桌子:
表:series
:
'id', 'series','thumbnail'
表:videos
:
'name','ref','sID'
sID是表id
中series
的外键。我想要表series
中的前5行,ref
中的每一行都有videos
。
然而,我的SQL:
SELECT `series` , `thumbnail` , `ref`
FROM `series`
LEFT JOIN `videos` ON `videos`.`sID` = `series`.`id`
ORDER BY `series`.`id` DESC
LIMIT 0 , 5
我最终获得了series
的第一行,但重复了5次,使用了不同的参考:
Youve Already Got It, image.jpg, fe4
Youve Already Got It, image.jpg, 79c57
Youve Already Got It, image.jpg, bd2
Youve Already Got It, image.jpg, ff15c
Youve Already Got It, image.jpg, 2ce
答案 0 :(得分:0)
试试:
SELECT
`series`,
`thumbnail`,
(SELECT `ref` FROM `videos` AS `v` WHERE `v`.`sID` = `s`.`id` LIMIT 0,1) AS `ref`
FROM `series` AS `s`
ORDER BY `series`.`id` DESC
LIMIT 0,5
答案 1 :(得分:0)
假设您知道自己在使用索引做什么;)
SELECT *, (SELECT ref FROM videos WHERE sID = id LIMIT 1) as first_ref
FROM series
ORDER BY id
LIMIT 5;
答案 2 :(得分:0)
你也可以尝试这样
SELECT `series` , `thumbnail` , `ref`
FROM `series`
INNER JOIN `videos` ON `videos`.`sID` = `series`.`id`
GROUP BY `series`.`id` DESC
LIMIT 0 , 5