0

我有两张桌子:

series::

'id', 'series','thumbnail'

videos::

'name','ref','sID'

sID 是idin table的外键series。我想要 table 的前 5 行,每个 fromseries的第一行。refvideos

但是,使用我的 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
4

3 回答 3

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
于 2013-05-28T11:25:49.000 回答
0

假设你知道你在用索引做什么;)

SELECT *, (SELECT ref FROM videos WHERE sID = id LIMIT 1) as first_ref
FROM series
ORDER BY id
LIMIT 5;
于 2013-05-28T11:26:31.510 回答
0

你也可以这样尝试

SELECT  `series` ,  `thumbnail` ,  `ref` 
FROM  `series` 
INNER JOIN  `videos` ON  `videos`.`sID` =  `series`.`id` 
GROUP BY  `series`.`id` DESC 
LIMIT 0 , 5
于 2013-05-28T11:34:08.860 回答