这是一个
greatest-n-per-group
问题;你需要找到的最小值
o
对于每个值
id_news
然后
JOIN
news_pic
对自己在
o
值匹配该最小值以获得第一张图片。请注意,您还有其他几个错误(
tc.flg_featured
应该是
tn.flg_featured
和
tc.date_news
应该是
tn.date_news
). 这应该会给你想要的结果:
SELECT
tn.date_news AS date_news,
tn.title AS title,
tn.text AS text,
tn.url AS url,
tc.name AS cat,
tp.file AS file
FROM news AS tn
JOIN news_cat AS tc ON tc.id_cat = tn.id_cat
JOIN news_pic tp ON tp.id_news = tn.id_news
JOIN (
SELECT id_news, MIN(o) AS o
FROM news_pic
GROUP BY id_news
) AS tpm ON tpm.id_news = tn.id_news AND tpm.o = tp.o
WHERE tn.flg_featured = 1
ORDER BY tn.date_news DESC
LIMIT 6
Demo on SQLFiddle