每条新闻都可以有多张图片,这些图片是按“ o ”字段排序的。
新闻
图片
此查询返回新闻标题列表以及缩略图的第一张图片。
SELECT n.title, p.filename
FROM news as n
LEFT JOIN picture AS p ON (p.id_news = n.id_news AND p.o = 1)
ORDER BY date_publish DESC
问题在于,由于CMS,如果管理员删除o = 1的图片,o = 2应该在上一个查询中进行。所以问题是:如何在新闻标题查询中添加第一张可用图片的文件名? (它应该是 o 字段中值最低的图片记录)
答案 0 :(得分:1)
如何在此处添加其他联接?
SELECT n.title, p.filename
FROM news as n
LEFT JOIN
picture p on
(p.id_news = n.id_news)
inner join
(select id_news,min(o) o from picture) AS p1
ON (p1.id_news = n.id_news AND p1.o = p.o)
ORDER BY n.date_publish DESC