我的MySql查询没什么问题: 我有两张桌子:
表时间表
id | date | text
1 2013-10-13 Hello
表 reps
id | date | text
1 2013-10-12 Its me again
1 2013-10-11 What?
1 2013-10-10 Lorem ipsum
我正在做的是UNION ALL
时间轴和reps.First行应始终是来自时间轴的行(它总是一行),然后是来自reps表的所有行,但是DESC
顺序。
我的查询是以下一个(除了顺序之外,它的工作正常)
select id,date,text from timeline UNION ALL select * from reps order by date desc
像评论一样思考(坐在最上面),对desc顺序的评论做出回复,最新的。
提前谢谢。
答案 0 :(得分:3)
将UNION放在子查询中:
SELECT id, date, text
FROM (SELECT id, date, text, 1 AS priority
FROM timeline
UNION ALL
SELECT *, 2 AS priority
FROM reps) u
ORDER BY priority, date DESC
答案 1 :(得分:1)
您的查询是正确的,您只需通过()关闭第二个查询:它将只排序第二个查询。没有()它会订购两个查询。
select id,date,text from timeline
UNION all
(select * from reps order by date desc)
答案 2 :(得分:0)
从日期值创建一个Unix_timestamp,并按其值排序。
SELECT -1 as 'id' , 99999999999999 as 'sortalias' , now() as 'datum||date'
UNION
( SELECT
id
,unix_timestamp(datum) as 'sortalias'
,datum as 'datum||date'
FROM kunde
)
order by sortalias desc
答案 3 :(得分:-1)
select id,date,text from (
select id,1 poid,date,text
from timeline
union all
select id,2 poid,date,text
from reps
) t
order by t.id asc,t.poid asc,t.date desc