Sql中写的顺序:
select [distinct]… from… join …on … where… group by [with cube|rollup]… having… union . order by… limit [offset,]
Sql中执行的顺序:
- FORM
- ON
- join
- where
- group by
- with cube|rollup
- having
- select
- distinct
- union
- order by
- limit
union和order by共存的问题:
order by只能存在于最后一个子查询的末尾,且不是对当前子查询的排序,而是对整个union后的结果集进行排序
如:
select * from jdbc_sink_1
union
select * from jdbc_sink_1_copy
order by id desc
若想存在于某个子查询则需要使用括号:
select * from (select * from jdbc_sink_1 order by id desc ) as a
union
select * from jdbc_sink_1_copy
jdbc_sink_1的数据:
jdbc_sink_1_copy的数据:
sql执行结果:
联合查询的结果是整个查询完成后得出的,而不是将子查询挨个完成后拼接的。由于我用的mysql版本是5.7,结果是这样,在5.6版本,结果中显示第一个子查询的结果是排序的