mysql 对同一个表进行合并后,如何使第一个查询结果排在最前面?

s4chpxco  于 12个月前  发布在  Mysql
关注(0)|答案(2)|浏览(81)

所以,我在同一个表上做联合,table1有2个不同的查询集。我的第一个查询应该返回类型为'test'的结果,第二个查询应该返回与类型无关的结果。但是当我在下面执行查询时,我的第一个查询结果不会出现在顶部。有没有办法实现这一点,使它总是显示第一个查询执行结果在顶部?

select * from table1 where tl='AAA' AND FT ='ABC' AND DATE='04-MAY-23 11:07:00' AND TYPE = 'TEST'
 UNION
 select * from table1  where tl='AAA' AND FT ='ABC' AND DATE='04-MAY-23 11:07:00'

字符串

qyswt5oh

qyswt5oh1#

希望下面的查询可以帮助,我们可以设置一个order by值到fixed_number字段,并可以使用该fixed_number字段执行order by

select *, 1 as fixed_number  from table1 where tl='AAA'  AND FT ='ABC' AND DATE='04-MAY-23 11:07:00' AND TYPE = 'TEST'
UNION
select *, 2 as fixed_number  from table1  where tl='AAA' AND FT ='ABC' AND DATE='04-MAY-23 11:07:00'
ORDER BY fixed_number;

字符串

uqcuzwp8

uqcuzwp82#

select * from table1 where tl='AAA' AND FT ='ABC' AND DATE='04-MAY-23 11:07:00' AND TYPE = 'TEST'
 UNION ALL
 select * from table1 where tl='AAA' AND FT ='ABC' AND DATE='04-MAY-23 11:07:00'

字符串
这给了我预期的结果。谢谢@Thorsten Kettner

相关问题