Mysql SELECT CASE LIKE 给出一个 NULL 行

Mysql SELECT CASE LIKE give a NULL row

我正在使用以下查询,但它给出了 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

添加 WHERE Browser IS NOT 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'
WHEN Browser IS NOT NULL
END AS Browser ,count(*) as num
FROM logs.log

group by Browser
order by num desc

您可以使用 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