xiaobin's studio

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

字数统计: 222阅读时长: 1 min
2015/12/04 Share
  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)
  1. 在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;

原文作者:小镔

原文链接:http://www.shaoxiaobin.com/2015/12/04/2015-12-04mysql 连接字符串为空时的处理/

发表日期:December 4th 2015, 9:00:00 am

更新日期:January 10th 2019, 11:30:41 pm

版权声明:非商业用途,转载请注明出处:邵小镔 http://www.shaoxiaobin.com

CATALOG