Mysql SELECT CASE LIKE 给出一个 NULL 行

Posted

技术标签:

【中文标题】Mysql SELECT CASE LIKE 给出一个 NULL 行【英文标题】:Mysql SELECT CASE LIKE give a NULL row 【发布时间】:2015-03-01 12:23:43 【问题描述】:

我正在使用以下查询,但它给出了 NULL 行以及其余结果:

SELECT  CASE 
WHEN csuseragent LIKE '%MSIE+8.0%' or csuseragent like '%Trident%' THEN 'IE'
WHEN csuseragent LIKE '%Firefox%' THEN 'Firefox'
WHEN csuseragent LIKE '%chrome%' THEN 'chrome'
END AS Browser ,count(*) as num
FROM logs.log
group by Browser
order by num desc

+---------+--------+
| Browser | num    |
+---------+--------+
| chrome  | 319607 |
| NULL    | 125974 |
| Firefox | 124627 |
| IE      |  56913 |
+---------+--------+

那么如何删除NULL125974

【问题讨论】:

【参考方案1】:

您可以使用having 子句过滤掉组:

SELECT  CASE 
WHEN csuseragent LIKE '%MSIE+8.0%' or csuseragent like '%Trident%' THEN 'IE'
WHEN csuseragent LIKE '%Firefox%' THEN 'Firefox'
WHEN csuseragent LIKE '%chrome%' THEN 'chrome'
END AS Browser ,count(*) as num
FROM logs.log
GROUP BY Browser
HAVING Browser IS NOT NULL
ORDER BY num DESC

【讨论】:

【参考方案2】:

给你

SELECT  CASE 
WHEN csuseragent LIKE '%MSIE+8.0%' or csuseragent like '%Trident%' THEN 'IE'
WHEN csuseragent LIKE '%Firefox%' THEN 'Firefox'
WHEN csuseragent LIKE '%chrome%' THEN 'chrome'
WHEN Browser IS NOT NULL
END AS Browser ,count(*) as num
FROM logs.log

group by Browser
order by num desc

【讨论】:

【参考方案3】:

添加WHERE Browser IS NOT NULL 子句

【讨论】:

以上是关于Mysql SELECT CASE LIKE 给出一个 NULL 行的主要内容,如果未能解决你的问题,请参考以下文章

select 中 like 变量 该怎么写?

关于sql查询中的like用法疑问?

Drupal 7 - 不区分大小写的 LIKE 与 db_select

SQL Like的用法

jsp select语句like用法

SQL语句中关于select……like的问题