我想确保在使用Union distinct时保留子查询结果的顺序。请注意,在进行联合时,需要使用“union distinct”来过滤重复项。MySQL - 在使用“Union distinct”构造时保留子查询记录的顺序
例如:
select columnA1, columnA2 from tableA order by [columnA3] asc
union distinct
select columnB1, columnB2 from tableB
当我运行此,我期待从子查询订购的记录(由[columnA3] ASC从TableA的顺序选择columnA1,columnA2)谁先(由顺序返回由columnA3 asc)接着是来自tableB的那些。
我假设我不能添加另一个虚拟列,因为这会使工会不同,不工作。所以,这是不行的:
select column1, column2 from
(select column1, column2, 1 as ORD from tableA order by [columnA3] asc
union distinct
select column1, column2, 2 as ORD from tableB
) order by ORD
为什么你不能添加一个虚拟列?它有什么问题? – Karolis
@Karolis来自tableA和tableB的记录的唯一性基于pair {column1。列2}。添加一个虚拟列(以确保顺序)使它们不唯一。 – user965692
是的,我明白你想从凯德的回答中做出什么:) – Karolis