我有以下查询,想要按CREATED列排序结果,我该怎么做呢?
(SELECT media.id, media.accessKey, media.internalName, media.type, media.modified, users.username, users.id, media.created,
0 AS reposted
FROM media
LEFT JOIN users ON users.id = media.userId)
UNION
(SELECT media.id, media.accessKey, media.internalName, media.type, media.modified, users.username, reposts.userId, reposts.created,
1 AS reposted
FROM reposts
LEFT JOIN media ON media.id = reposts.mediaId
LEFT JOIN users ON users.id = reposts.userId)
答案 0 :(得分:1)
添加:
order by created
在查询结束时。
答案 1 :(得分:1)
您只需要在查询中添加order by子句(在查询周围使用(
不是必需的):
SELECT media.id,
media.accessKey,
media.internalName,
media.type,
media.modified,
users.username,
users.id,
media.created,
0 AS reposted
FROM media
LEFT JOIN users ON users.id = media.userId
UNION
SELECT media.id,
media.accessKey,
media.internalName,
media.type,
media.modified,
users.username,
reposts.userId,
reposts.created,
1 AS reposted
FROM reposts
LEFT JOIN media ON media.id = reposts.mediaId
LEFT JOIN users ON users.id = reposts.userId
ORDER BY CREATED ASC
您可以查看此sqlfiddle以查看其是否有效。