这是我的代码: 'media_work'表有一个'order'字段,表示文件的顺序。 我想对'media_work'表的结果进行排序以获得第一项(图像)
此代码有效,但未订购'media_work'
SELECT work.id, work.title, media.file_name_thumb,media_work.order,
COUNT(media.file_name_thumb) AS mycount
FROM work
LEFT OUTER JOIN media_work ON media_work.work_id = work.id
LEFT OUTER JOIN media ON media_work.media_id = media.id
GROUP BY work.id ORDER BY work.id DESC
这是结果
id title file_name_thumb order mycount
-------------------------------------------------------------------
3 laatste thumb_20101028134738_278.gif 1 3
2 test work add thumb_20101028133811_893.gif 1 56
1 test werk thumb_20101028132316_537.jpg 2 5
我希望订单按顺序升序,因此它将为'0'
答案 0 :(得分:2)
如果您想为每个media
选择第一个work
:
SELECT work.id, work.title, media.file_name_thumb,
(
SELECT COUNT(*)
FROM media_work
WHERE media_work.work_id = work.id
)
FROM work
LEFT JOIN
media
ON media.id =
(
SELECT media_work.media_id
FROM media_work
WHERE media_work.work_id = work.id
ORDER BY
media_work.work_id, media_work.order
LIMIT 1
)
在media_work (work_id, order)
上创建一个索引,以便快速工作。