ORDER BY datediff()在mariaDB中以未命名的列反转

时间:2018-02-08 03:56:30

标签: sql mariadb

以下示例查询,您能告诉我他们为什么会返回不同的结果吗?具体来说,为什么订单被逆转。

这两个之间只有一个区别:在第二个查询中,select子句中的datediff被命名并在ORDER BY中重新使用,而在select Start_Date, min(End_Date), datediff(min(End_Date), Start_Date) from ( select Start_Date from Projects where Start_Date not in (select End_Date from Projects) ) a, (select End_Date from Projects where End_Date not in (select Start_Date from Projects) ) b where Start_Date < End_Date group by Start_Date order by datediff(min(End_Date), Start_Date) ; +------------+---------------+-------------------------------------+ | Start_Date | min(End_Date) | datediff(min(End_Date), Start_Date) | +------------+---------------+-------------------------------------+ | 2015-10-01 | 2015-10-04 | 3 | | 2015-10-13 | 2015-10-15 | 2 | | 2015-10-28 | 2015-10-29 | 1 | | 2015-10-30 | 2015-10-31 | 1 | +------------+---------------+-------------------------------------+ 中第一个没有命名。

这是使用mariadb 10.1.18以及10.2.12。

首次查询

select Start_Date, min(End_Date), datediff(min(End_Date), Start_Date) as 'test_diff'
from (
 select Start_Date 
 from Projects 
 where Start_Date 
 not in (select End_Date from Projects)
) a,
(select End_Date 
 from Projects 
 where End_Date 
 not in (select Start_Date from Projects)
) b
where Start_Date < End_Date
group by Start_Date
order by test_diff
;

+------------+---------------+-----------+
| Start_Date | min(End_Date) | test_diff |
+------------+---------------+-----------+
| 2015-10-28 | 2015-10-29    |         1 |
| 2015-10-30 | 2015-10-31    |         1 |
| 2015-10-13 | 2015-10-15    |         2 |
| 2015-10-01 | 2015-10-04    |         3 |
+------------+---------------+-----------+

第二次查询:

HOST_COHERENT

2 个答案:

答案 0 :(得分:0)

您的第二个查询

order by test_diff

并且你的第一个不在你将第一行显示为第二行。 如果您将第二个查询的订单更改为

order by test_diff DESC

它看起来像第一个,将结果放入DESCending Order

答案 1 :(得分:0)

听起来像个臭虫。请提交错误报告。

同时,通过创建大部分查询的子查询,然后在外部查询中执行ORDER BY,可能会解决问题。