5.mysql 连接字符串为空时的处理

  1. 查出字段为空的判断
    • isnull
      isnull(xxxx, ‘default’),可以使用isnull函数进行判断,‘default’ 为替换字符,select * from table where isnull(xxx,'') = ''
    • length
      length(xxxx) 函数判断查询出的字段的长度进行判断;
    • is not null
      在语句中包含 select * from table where xxx is not null
    • caue when
      使用case when 语句 (cause when xx is null or xx = '' then '为空后返回该字符串' else xx)
    • case when + count
      加上count函数进行判断(cause when count(xx) = 0 then '为空后返回该字符串' else xx)
  2. 在sql查询中有些时候就要使用concat函数进行字符串的拼接,
    例如:
SELECT CONCAT(a.str1,b.str1) FROM
(SELECT 
    (CASE WHEN count(elmtGroupName) = 0 THEN '' ELSE CONCAT(elmtGroupName,':',
    (CASE WHEN count(priceImpact) = 0 THEN '' ELSE priceImpact  END),';') END)
     as str1,elmtGroupName FROM report_otherinfo
WHERE elmtGroupName LIKE '主原料%') AS a,
(SELECT 
    (CASE WHEN count(elmtGroupName) = 0 THEN '' ELSE CONCAT(elmtGroupName,':',
    (CASE WHEN count(priceImpact) = 0 THEN '' ELSE priceImpact  END),';') END)
 as str1,elmtGroupName FROM report_otherinfo
WHERE elmtGroupName LIKE '按时%') AS b;

同时发布在:shaoxiaobin

你可能感兴趣的:(web前段学习笔记)