I have events table with columns 'title', 'description', 'start_date', 'end_date'.
I want to get ordered list live and future events, depends on 'start_date' and 'end_date'.
I try
(
select *
from `events`
where `start_date` < NOW() and `end_date` > NOW()
order by `start_date` desc
)
union all
(
select *
from `events`
where `start_date` > NOW()
order by `start_date` desc)
but result have not that ordering which I want. I want at first ordered list by start_date live events after that ordered list by start_date future events.
答案 0 :(得分:2)
我认为你不需要在这个联盟。
只需在基本查询中添加OR
条件。
然后,要将结果拆分为"this one is live"
和"this one is future"
,您可以使用标记
SELECT *, start_date < NOW() AS flag
FROM `events`
WHERE (`start_date` < NOW() and `end_date` > NOW())
OR `start_date` > NOW()
ORDER BY flag DESC, start_date
Nota:DESC
在此向您展示您希望在未来活动之前上线。否则只需放ASC
。
答案 1 :(得分:1)
试试这个:
select * from (
(
select start_date, end_date, 'Live' as event_type
from `events`
where `start_date` < NOW() and `end_date` > NOW()
)
union
(
select start_date, end_date, 'Future' as event_type
from `events`
where `start_date` > NOW() ) ) a
ORDER BY event_type desc
, case when event_type = 'Live' then start_date end desc
, case when event_type = 'Future' then start_date end asc;
答案 2 :(得分:0)
为什么不能选择start_date
订购的所有活动?
select *
from `events`
where (`start_date` < NOW() AND `end_date` > NOW())
or (`start_date` > NOW())
order by `start_date` desc
无论如何,直播活动将是第一次,然后是未来活动