mySQL中查询统计俩个表相加COUNT之合

最近工作中需要用到查询语句结果为统计当前表和历史表加起来的数据之和需要把俩条数据的合加起来返回给我们:
select count() from work_order_history where valid = true and and state = ‘8’ and create_time like ‘%20230901%’;
返回结果如下
在这里插入图片描述
另一条SQL如下:
select count(
) from work_order where valid = true and and state = ‘8’ and create_time like ‘%20230901%’;
返回结果如下:
在这里插入图片描述
这里需要将0和1加起来,因为我的们的参数还要替换别的状态,这里展示的总数可能不明显

最后我们需要加起来的SQL为

select SUM(z.a) from (
select count() as a from select count() from work_order where valid = true and and state = ‘8’ and create_time like ‘%20230901%’ union all
select count() from work_order_history where valid = true and and state = ‘8’ and create_time like ‘%20230901%’
)z
SUM(z.a)这里如果查出来的结果想转换格式,我们可以换成cast(SUM(z.a) as CHAR)如下:
select cast(SUM(z.a) as CHAR) from (
select count(
) as a from select count() from work_order where valid = true and and state = ‘8’ and create_time like ‘%20230901%’ union all
select count(
) from work_order_history where valid = true and and state = ‘8’ and create_time like ‘%20230901%’
)z

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值