Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql Order by不能与Union ALL my sql一起正常工作_Mysql_Sql Order By_Union - Fatal编程技术网

Mysql Order by不能与Union ALL my sql一起正常工作

Mysql Order by不能与Union ALL my sql一起正常工作,mysql,sql-order-by,union,Mysql,Sql Order By,Union,我有一个查询,我想在哪里列出结果,如下所示: 我使用了下面的查询来实现它,但它并没有按预期工作,并对结果进行了ASC或DESC排序 SELECT * from (SELECT eventid, eventdate,eventdates FROM events WHERE events.onlineclosingdate>curdate() order by onlineclosingdate asc) a UNION ALL SELECT * from (SELECT eventid

我有一个查询,我想在哪里列出结果,如下所示:

我使用了下面的查询来实现它,但它并没有按预期工作,并对结果进行了ASC或DESC排序

SELECT * from (SELECT eventid, eventdate,eventdates FROM events WHERE events.onlineclosingdate>curdate() order by onlineclosingdate asc) a
UNION  ALL
SELECT * from (SELECT eventid, eventdate,eventdates FROM events WHERE events.onlineclosingdate<=curdate() order by onlineclosingdate desc) as c 

联合两个查询并在一个查询中分组


例如,您可能需要标识2个日期周期块并使用条件顺序 给定


写下完整的查询。我的查询中确实缺少什么?请告诉我..?按eventid订购?为什么按eventid订购?当我不需要的时候。。我只需要按日期排序。我在下面写下答案。如果你不需要,请忽略排序。那么eventdates不是日期类型?它是日期类型请正确检查列名。是eventdates是日期类型吗?我通过在线关闭日期列bro来实现订单
SELECT * FROM (
SELECT a.eventid as `eventid`, a.eventdate as `eventdate`, a.eventdates as `eventdates`  FROM events a WHERE a.onlineclosingdate>curdate() order by a.onlineclosingdate asc)
UNION  ALL
SELECT c.eventid as `eventid`, c.eventdate as `eventdate`, c.eventdates as `eventdates` FROM events c WHERE c.onlineclosingdate<=curdate() order by c.onlineclosingdate desc)
) as `all` 
drop table if exists t;
create table t (id int auto_increment primary key, dt date);

insert into t (dt) values
('2017-02-01'),('2017-10-01'),('2017-01-01'),
('2016-02-01'),('2016-10-01'),('2016-01-01');

select s.id,s.dt 
from
(
select 1 as srce,id,t.dt dt from t where dt > str_to_date('2016-12-31','%Y-%m-%d') 
union all
select 2,id,t.dt from t where dt <= str_to_date('2016-12-31','%Y-%m-%d') 
) s
order by srce asc,
            case when s.srce = 1 then s.dt end asc, 
            case when s.srce = 2 then s.dt end desc;

+----+------------+
| id | dt         |
+----+------------+
|  3 | 2017-01-01 |
|  1 | 2017-02-01 |
|  2 | 2017-10-01 |
|  5 | 2016-10-01 |
|  4 | 2016-02-01 |
|  6 | 2016-01-01 |
+----+------------+
6 rows in set (0.00 sec)