mysql的case when字段为空,null的问题

2022-12-17

这篇文章主要介绍了mysql的case when字段为空,null的问题。具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

目录
  • mysql的case when字段为空,null
  • case when 判断null(已测试)
  • 总结

mysql的case when字段为空,null

name字段为null时替换为 ‘该字段为空’

SELECT 
CASE 
WHEN name is null THEN '该字段为空'
ELSE name
END as name
,email_id
 FROM mysql_table_name

case when 判断null(已测试)

SELECT
		(case when cdi.DEVICE_TAG is not null then cdi.DEVICE_TAG else '暂无数据' end) as deviceTag,
		(case when cdi.DEVICE_CODE is not null then cdi.DEVICE_CODE else '暂无数据' end) as deviceCode,
		(case when sc.full_name is not null then sc.full_name else '暂无数据' end) as deviceVendor,
		(case when ct.CLASS_NAME is not null then ct.CLASS_NAME else '暂无数据' end) as deviceTypeNameB,
		(case when cdi.DEVICE_PORT is not null then cdi.DEVICE_PORT else '暂无数据' end) as devicePort,
		(case when cdi.CREATE_TIME is not null then cdi.CREATE_TIME else '暂无数据' end) as createTimeStr
-- 			cdi.DEVICE_TAG as deviceTag,
-- 			cdi.DEVICE_CODE as deviceCode,
-- 			sc.full_name as deviceVendor,
-- 			ct.CLASS_NAME as deviceTypeNameB,
-- 			cdi.DEVICE_PORT as devicePort,
-- 			cdi.CREATE_TIME as createTimeStr
		FROM
			con_device_info AS cdi
		LEFT JOIN sys_code AS sc ON sc.code_value = cdi.DEVICE_VENDOR
		LEFT join con_type as ct on ct.ID=cdi.DEVICE_TYPE
		where cdi.ID=#{id}

重点:

case when xxx

is null 或者is not null

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持北冥有鱼。