【发布时间】:2020-08-02 13:48:37
【问题描述】:
我有以下几点:
with
result1 as ( select column1 from table1 order by ... )
result2 as ( select column1 from table2 order by ... )
select * from result1 union all select * from result2;
目的是列出result1保留其顺序,然后是result2保留其顺序。但是,union all 似乎没有保留其操作数表的顺序。我怎样才能达到预期的效果?我尝试将列添加到已排序的 result1 和 result2 中,然后在此添加的列上排序联合结果,但这对于看起来非常典型的东西来说似乎非常笨拙。有union all ordered之类的吗?
【问题讨论】:
-
任何没有 ORDER BY 子句的结果集都是无序的。这同样适用于
select * from result1 union all select * from result2;。 -
添加到您的 ORDER BY a LIMIT 18446744073709551615 中,这样优化器就不会删除订单
-
选择 1 seq,table1 union 中的其他列选择 2,table 2 中的其他列...
标签: mysql sql-order-by union