简体   繁体   English

mysql的联合声明

[英]mysql union statement with order by

I'am trying to understand what causes the following, maybe you could help me: 我试图了解导致以下情况的原因,也许您可​​以帮助我:

I have a query like: 我有一个查询,如:

 select field1,fieldDate from table1 
 union all 
 select field1,fieldDate from table2
 order by fieldDate desc

and the another one like this: 另一个像这样:

 select field1,field2,fieldDate from table1 
 union all 
 select field1,field2,fieldDate from table2 
 order by fieldDate desc

So basically they are the same with the exception that in the second I retrieve an extra field. 因此基本上它们是相同的,除了第二个是我检索了一个额外的字段。

Now, both results come with a diferent ordering, but just for the cases that the dates are exacly the same. 现在,两个结果都具有不同的顺序,但是仅对于日期完全相同的情况。 For example there are 2 rows (row1,row2) with date 2009-11-25 09:41:55. 例如,有2行(行1,行2)的日期为2009-11-25 09:41:55。 For query 1 row1 comes before row2 and for query 2 row2 comes before row1. 对于查询1,row1在row2之前,对于查询2,row2在row1之前。 Does somebody knows why this happens? 有人知道为什么会这样吗?

Thanks, Regards 感谢和问候

The ordering based on any fields that you don't explicitly order by is undefined, and the optimizer can change the ordering if it thinks that results in a better execution plan. 未定义您未明确排序依据的任何字段的排序,并且如果优化器认为这样做会导致更好的执行计划,则可以更改该排序。 Given two rows with the exact same value in the order by field you can not depend on them being in any particularly order in relation to each other unless you explicitly order by another field with different values. 给定两行在按字段排序时具有完全相同的值,除非您按另一个具有不同值的字段显式排序,否则不能依赖于它们之间是否具有任何特定的顺序。

Can you do this 你能做这个吗

  select * from ( select 
     field1,field2,fieldDate, 0 as ordercol from table1 
    union all select 
     field1,field2,fieldDate, 1 as ordercol from table2) t1
    order by fieldDate desc, ordercol asc

Straight from the MySQl manual, to user order by on a union you have to parenthesis the individual tables. 直接从MySQl手册开始,根据用户要求(通过并集),您必须在各个表中加上括号。

(select field1,fieldDate from table1)
union all 
(select field1,fieldDate from table2)
order by fieldDate desc

This is not SQL standards compliant! 这不符合SQL标准! The code you entered should order the union of both tables but to my surprise MySQL has the above syntax. 您输入的代码应该对两个表进行联合,但是令我惊讶的是MySQL具有上述语法。

The order in which rows with the same fieldDate are returned can differ for each query execution. 对于每次查询执行,返回具有相同fieldDate的行的顺序可以不同。 Usually this order will be the same but you should not count on it. 通常,此顺序是相同的,但您不应指望它。 If you want any extra ordering state more order by fields. 如果您需要任何其他订购状态,请按字段订购更多。

EDIT: This answer is wrong: the order by works on the entire union. 编辑:这个答案是错误的:该命令由对整个联合的工作。 I'll leave it here to save others the trouble :) 我将其留在这里以免其他人遇到麻烦:)


Your order by only works on the second part of the union. 您的订单仅在联合的第二部分起作用。 You can use a subquery to make the order by work on the entire union: 您可以使用子查询通过对整个联合的工作来进行订购:

select field1,field2,fieldDate 
from (
    select field1,field2,fieldDate 
    from table1 
    union all 
    select field1,field2,fieldDate 
    from table2
) SubQueryName
order by fieldDate desc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM