MySQL:在逐个语句之前对结果进行排序

时间:2017-08-22 18:54:44

标签: mysql

基本上,我有一个铜矿画廊,我想在主页上显示最近4张更新的专辑。这是我到目前为止的查询。它基本上得到了最新的图片。子查询可以自行运行,但是当需要对它们进行分组以便自己获得每个专辑时,它似乎没有从列表中获取最新的专辑。

SELECT * 
FROM (
 SELECT c.cid, c.name AS catname, a.aid, a.title AS albumtitle, a.category, p.aid AS albumid,p.filepath,p.filename,p.ctime AS creationtime,p.title AS pictitle,p.approved
 FROM cpg145_pictures AS p LEFT JOIN `cpg145_albums` AS a ON p.aid = a.aid LEFT JOIN `cpg145_categories` AS c ON a.category = c.cid
 WHERE p.approved='YES' AND a.category IN (47,48)
 ORDER BY p.ctime DESC) AS T
GROUP BY albumid
ORDER BY creationtime DESC
LIMIT 4

1 个答案:

答案 0 :(得分:0)

我想出了答案。显然,在MariaDB中,您必须为子查询提供正确排序的限制。所以:

SELECT * 
FROM (
 SELECT c.cid, c.name AS catname, a.aid, a.title AS albumtitle, a.category, p.aid AS albumid,p.filepath,p.filename,p.ctime AS creationtime,p.title AS pictitle,p.approved
 FROM cpg145_pictures AS p LEFT JOIN `cpg145_albums` AS a ON p.aid = a.aid LEFT JOIN `cpg145_categories` AS c ON a.category = c.cid
 WHERE p.approved='YES' AND a.category IN (47,48)
 ORDER BY p.ctime DESC
 LIMIT 200) AS T
GROUP BY albumid
ORDER BY creationtime DESC
LIMIT 4