MYSQL COUNT/SUM IF/IFNULL 结合使用

1、IF(条件,值1,值2) 如果条件为true,则IF()的返回 (值1),否则返回(值2)
2、IFNULL(expr1,expr2) 假如expr1不为NULL,则IFNULL()的返回值为expr1;否则其返回值为expr2
实际代码:

select
			COUNT(IF(e.`level` = 1, 1, NULL)) lv1,
			COUNT(IF(e.`level` = 2, 1, NULL)) lv2,
			COUNT(IF(e.`level` = 3, 1, NULL)) lv3,
			COUNT(IF(e.`level` = 4, 1, NULL)) lv4,
			COUNT(IF(e.`level` = 5, 1, NULL)) lv5,
			COUNT(IF(e.`level` = 6, 1, NULL)) lv6,
			COUNT(IF(e.`level` = 7, 1, NULL)) lv7,
			COUNT(IF(e.`level` = 8, 1, NULL)) lv8,
			COUNT(IF(e.`level` = 9, 1, NULL)) lv9,
			COUNT(IF(sc.score = 3, 1, NULL)) 's3',
			<if test="pd.pay_flag != null and pd.pay_flag != '' and !pd.isAdminG">
			SUM(IF(e.`level` = 1, (IFNULL(prd.pay_reg_fee,0) + IFNULL(prd.pay_other_fee,0) + IFNULL(prd.pay_other_fee_center,0)), 0)) fee1,
			SUM(IF(e.`level` = 2, (IFNULL(prd.pay_reg_fee,0) + IFNULL(prd.pay_other_fee,0) + IFNULL(prd.pay_other_fee_center,0)), 0)) fee2,
			SUM(IF(e.`level` = 3, (IFNULL(prd.pay_reg_fee,0) + IFNULL(prd.pay_other_fee,0) + IFNULL(prd.pay_other_fee_center,0)), 0)) fee3,
			SUM(IF(e.`level` = 4, (IFNULL(prd.pay_reg_fee,0) + IFNULL(prd.pay_other_fee,0) + IFNULL(prd.pay_other_fee_center,0)), 0)) fee4,
			SUM(IF(e.`level` = 5, (IFNULL(prd.pay_reg_fee,0) + IFNULL(prd.pay_other_fee,0) + IFNULL(prd.pay_other_fee_center,0)), 0)) fee5,
			SUM(IF(e.`level` = 6, (IFNULL(prd.pay_reg_fee,0) + IFNULL(prd.pay_other_fee,0) + IFNULL(prd.pay_other_fee_center,0)), 0)) fee6,
			SUM(IF(e.`level` = 7, (IFNULL(prd.pay_reg_fee,0) + IFNULL(prd.pay_other_fee,0) + IFNULL(prd.pay_other_fee_center,0)), 0)) fee7,
			SUM(IF(e.`level` = 8, (IFNULL(prd.pay_reg_fee,0) + IFNULL(prd.pay_other_fee,0) + IFNULL(prd.pay_other_fee_center,0)), 0)) fee8,
			SUM(IF(e.`level` = 9, (IFNULL(prd.pay_reg_fee,0) + IFNULL(prd.pay_other_fee,0) + IFNULL(prd.pay_other_fee_center,0)), 0)) fee9,
			SUM(IF(sc.score = 3, (IFNULL(prd.pay_reg_fee,0) + IFNULL(prd.pay_other_fee,0) + IFNULL(prd.pay_other_fee_center,0)), 0)) feeS3,
			</if>
			s.id subject_id,
			s.subject_nm
		from
			t_exam e
			inner join m_subject s on e.subject_id = s.id
        	inner join r_area_agency_user aau on e.ar_ag_u_id = aau.id
        	left join t_score sc on sc.exam_id = e.id
        	<if test="pd.pay_flag != null and pd.pay_flag != ''">
			left join (SELECT DISTINCT pr.pay_other_fee,pr.pay_other_fee_center,pr.pay_reg_fee,pr.exam_id 
				FROM t_pay_record_detail pr INNER JOIN t_pay_record pd ON pd.id = pr.pay_record_id 
				WHERE pd.pay_flag=1 
				GROUP BY pr.exam_id)prd ON prd.exam_id = e.id
			</if>
		<where>
				AND e.delete_flag = 0
			<if test="pd.pay_flag != null and pd.pay_flag != ''">
				and e.pay_flag= #{pd.pay_flag}
			</if>
			<if test="pd.keywords!= null and pd.keywords != ''"><!-- 关键词检索 -->
				and
					(
					e.batch_no LIKE CONCAT(CONCAT('%', #{pd.keywords}),'%')
					or
					e.exam_id LIKE CONCAT(CONCAT('%', #{pd.keywords}),'%')
					or 
					e.id LIKE CONCAT(CONCAT('%', #{pd.keywords}),'%')
					)
			</if>
			<if test="pd.start_date!=null and pd.start_date!=''">
				and convert(e.create_dt,date)&gt;=convert(#{pd.start_date},date)
			</if>
			<if test="pd.end_date!=null and pd.end_date!=''">
				and convert(e.create_dt,date)&lt;=convert(#{pd.end_date},date)
			</if>
			<if test="pd.period_ids != null and pd.period_ids != ''">
				and e.period_id in 
				<foreach item="item" index="index" collection="pd.period_id_list" open="(" separator="," close=")">
					${item}
				</foreach>
			</if>
			<if test="pd.agency_id!=null and pd.agency_id!=''">
				and aau.agency_id =#{pd.agency_id}
			</if>
			<if test="pd.area_id!= null and pd.area_id != ''">
				and aau.area_id = #{pd.area_id}
			</if>
			<if test="pd.batch_no != null and pd.batch_no != ''">
				and e.batch_no = #{pd.batch_no}
			</if>
			<if test="pd.timesId != null and pd.timesId != ''">
				and e.exam_times_id = #{pd.timesId}
			</if>
		</where>
		GROUP BY e.subject_id
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
ifnull函数在MySQL中用于判断一个字段是否为null,如果为null则返回指定的默认值。根据引用\[1\]和引用\[2\]的内容,可以看出ifnull函数在某些情况下可能不生效。比如,当查询的记录本身就不存在时,ifnull函数无法对null结果进行转换。在这种情况下,可以使用其他函数如countsum来构造一个有记录但字段为null的情况,从而使ifnull函数生效。举例来说,可以使用类似以下的语句来解决ifnull不生效的问题: SELECT IFNULL((SELECT score FROM student WHERE studentid = 8), 0); 这样,如果student表中不存在studentid为8的记录,ifnull函数会将null转换为0返回。\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* [mysql函数IFNULL判空不生效](https://blog.csdn.net/json8888/article/details/121560082)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [mysql ifnull不起作用原因及解决方案](https://blog.csdn.net/shuair/article/details/120703082)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值