mySql中union all 及max,ifnull用法

本文介绍了MySQL中UNION ALL结合MAX和IFNULL的使用方法。重点包括:1) 使用UNION ALL连接查询时,字段数量必须一致,空值可用''填充;2) 利用IFNULL函数处理NULL值,例如IFNULL(d.money, 0)作为totalMoney;3) 当需要合并多条记录为一条时,需对所有字段应用MAX函数,如max(d.orderNum),以避免字段值为空的情况。" 113578766,10547992,Vue3 watch与watchEffect深度解析,"['Vue.js', '前端开发', 'Vue3 API']
摘要由CSDN通过智能技术生成

mySql中union all 及max,ifnull用法

select
	ifnull(max(d.orderNum),0) totalNum,
	ifnull(max(d.money),0) totalMoney,
	ifnull(max(d.unpayOrderNum),0) unpaidNum,
	ifnull(max(d.unpayMoney),0) unpaidMoney,
	ifnull(max(d.payOrderNum),0) paidNum,
	ifnull(max(d.PayMoney),0) paidMoney from 
		(SELECT
			count(1) orderNum,
			sum(money) money,
			'' unpayOrderNum,
			'' unpayMoney,
			'' payOrderNum,
			'' PayMoney
		from 
			os_order_form
		where del_flag = 0
		UNION ALL
		SELECT
			'' orderNum,
			'' money,
			count(1) unpayOrderNum,
			sum(money) unpayMoney,
			'' payOrderNum,
			'' PayMoney
		from 
			os_order_form
		where 
			del_flag = 0 
			and 
			pay_time is null
		UNION ALL
	 SELECT
			'' orderNum,
			'' money,
			'' unpayOrderNum,
			'' unpayMoney,
			count(1) payOrderNum,
			sum(money) PayMoney
		from 
			os_order_form
		
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值