mysql 将两个SQL语句查询结果并在一起

mysql 将两个SQL语句查询结果并在一起,第1张

需要将结果合并到一起显示出来,这个时候

就需要用到union和union all关键字来实现这样的功能,union和union all的主要区别是union all是把结果集直接合并在一起,而

union 是将union all后的结果进行一次distinct,去除重复的记录后的结果。

两个一模一样的 语句 union all 得到的结果 是两份数据,

你的问题是 union all 与 order by 不能一起使用

需要 改成:

1、

select * from (select * from mallbuilder_product where flash_sale=1 and FROM_UNIXTIME(end_time,'%Y-%m-%d %H:%m:%s')>NOW() ORDER BY flash_sale_date ASC) a

union all

select * from (select * from mallbuilder_product where flash_sale=1 and

FROM_UNIXTIME(end_time,'%Y-%m-%d %H:%m:%s')>NOW() ORDER BY

flash_sale_date ASC) b

或者

select * from (

select * from mallbuilder_product where flash_sale=1 and

FROM_UNIXTIME(end_time,'%Y-%m-%d %H:%m:%s')>NOW()

union all

select * from mallbuilder_product where flash_sale=1 and

FROM_UNIXTIME(end_time,'%Y-%m-%d %H:%m:%s')>NOW()

)ORDER BY

flash_sale_date ASC

还有,不明白的是,为什么要两个一样的语句 union all,没猜错的话, where 条件 =2

请参考下列sql语句

select

(select count(*) from 用户表 where

状态='冻结') as 冻结用户数,

(select count(*) from 用户表 where

性别='男') as 男用户数


欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/zaji/6105134.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-14
下一篇 2023-03-14

发表评论

登录后才能评论

评论列表(0条)

保存