嘿,我有一个按日期排序的查询,我希望日期与添加的时间顺序相同,以保持稳定的结果。
我有以下查询:
(
SELECT 'album' AS RowType, id, DATE, title, time_added
FROM albums
WHERE is_temp =0
AND subject_id = '3'
)
UNION ALL (
SELECT 'video' AS RowType, id, DATE, title, time_added
FROM videos
WHERE is_temp =0
AND subject_id = '3'
)
UNION ALL (
SELECT 'story' AS RowType, id, DATE, title, time_added
FROM stories
WHERE is_temp =0
AND subject_id = '3'
)
ORDER BY DATE, time_added
哪个效果很好但我不想要选择time_added
列,因为我在结果集中不需要它。
我可以以某种方式实现这一目标吗?如果我删除time_added,查询将无法运行。
我找到了以下内容:
ORDER BY子句导致输出行被排序 ORDER BY的参数是一个用作表达式的表达式列表 排序的关键。表达式不必是结果的一部分 对于一个简单的SELECT,但在复合SELECT中每个排序表达式 必须与其中一个结果列完全匹配。每种排序表达都可以 可选地后跟一个COLLATE关键字和一个名称 用于订购文本和/或关键字ASC或DESC的整理功能 指定排序顺序。
我无法决定是否意味着它是不可能的,或者我应该更加努力。
答案 0 :(得分:0)
试试这个
SELECT RowType
, id
, DATE
, title
FROM
(
SELECT 'album' AS RowType , id , DATE , title , time_added
FROM albums
WHERE is_temp = 0 AND subject_id = '3'
UNION ALL
SELECT 'video' AS RowType , id , DATE , title , time_added
FROM videos
WHERE is_temp = 0 AND subject_id = '3'
UNION ALL
SELECT 'story' AS RowType , id , DATE , title , time_added
FROM stories
WHERE is_temp = 0 AND subject_id = '3'
) s
ORDER BY DATE , time_added