MySQL如何将WHERE子句中未指定的值分组为“其他”?

发布于 2025-01-06 06:22:13 字数 270 浏览 0 评论 0原文

我需要制作一份网站访问量的统计报告。在报告中,用户可以指定各种参数。到目前为止,每当他们为参数选择值时,就会添加一个额外的 WHERE 子句,如下所示:

SELECT id_browser, COUNT(visits)
FROM table
WHERE [...] 
AND id_browser IN (1, 3, 6)
GROUP BY id_browser

但我真正需要做的是显示其余浏览器的统计信息,但我需要将它们分组为“其他”浏览器”。我该怎么做?

I need to produce a statistical report of the site visits. In the report users can specify various parameters. So far, whenever they select values for a parameter, an additional WHERE clause is added, like this:

SELECT id_browser, COUNT(visits)
FROM table
WHERE [...] 
AND id_browser IN (1, 3, 6)
GROUP BY id_browser

But what I really need to do is to show the statistics for the remaining browsers as well, but I need to group them together as "other browsers". How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

-小熊_ 2025-01-13 06:22:13

尝试以下:

    SELECT id_browser, 
    count(if(id_browser IN (1, 3, 6),1,0)) as 136browser,
    count(if(id_browser Not IN (1, 3, 6),1,0)) as other_browser
    FROM table
    WHERE [...] 
    GROUP BY id_browser

Try below :

    SELECT id_browser, 
    count(if(id_browser IN (1, 3, 6),1,0)) as 136browser,
    count(if(id_browser Not IN (1, 3, 6),1,0)) as other_browser
    FROM table
    WHERE [...] 
    GROUP BY id_browser
浪漫之都 2025-01-13 06:22:13

使用 CASE 语法:

例如,

SELECT CASE
           WHEN id_browser = 1 THEN 'IE'      -- I am just inventing
           WHEN id_browser = 3 THEN 'Chrome'  -- some mappings
           WHEN id_browser = 6 THEN 'Firefox' -- for these
           ELSE 'All Other Browsers'
       END
       ,COUNT(visits)
 FROM [table]
WHERE [...] 
GROUP BY CASE
           WHEN id_browser = 1 THEN 'IE'
           WHEN id_browser = 3 THEN 'Chrome'
           WHEN id_browser = 6 THEN 'Firefox'
           ELSE 'All Other Browsers'
       END

Use CASE syntax:

e.g.,

SELECT CASE
           WHEN id_browser = 1 THEN 'IE'      -- I am just inventing
           WHEN id_browser = 3 THEN 'Chrome'  -- some mappings
           WHEN id_browser = 6 THEN 'Firefox' -- for these
           ELSE 'All Other Browsers'
       END
       ,COUNT(visits)
 FROM [table]
WHERE [...] 
GROUP BY CASE
           WHEN id_browser = 1 THEN 'IE'
           WHEN id_browser = 3 THEN 'Chrome'
           WHEN id_browser = 6 THEN 'Firefox'
           ELSE 'All Other Browsers'
       END
贱贱哒 2025-01-13 06:22:13

您可以使用 UNION 合并结果,

试试这个:

SELECT *
FROM
(
    SELECT id_browser, COUNT(visits)
    FROM table
    WHERE [...] 
    AND id_browser IN (1, 3, 6)
    GROUP BY id_browser
         UNION
    SELECT 'Other Browser' as id_browser, COUNT(visits)
    FROM table
    WHERE [...] 
    AND id_browser NOT IN (1, 3, 6)
) AllResult

You can unite the results using UNION

try this:

SELECT *
FROM
(
    SELECT id_browser, COUNT(visits)
    FROM table
    WHERE [...] 
    AND id_browser IN (1, 3, 6)
    GROUP BY id_browser
         UNION
    SELECT 'Other Browser' as id_browser, COUNT(visits)
    FROM table
    WHERE [...] 
    AND id_browser NOT IN (1, 3, 6)
) AllResult
静若繁花 2025-01-13 06:22:13

听起来这是一个简单的联合:

select id_browser, count(visits) from table
where [...] and id_browser in (1, 3, 6)
group by id_browser
union
select 'Other browsers', count(visits) from table
where [...] and id_browser not in (1, 3, 6)
group by id_browser

Sounds like it is a simple union:

select id_browser, count(visits) from table
where [...] and id_browser in (1, 3, 6)
group by id_browser
union
select 'Other browsers', count(visits) from table
where [...] and id_browser not in (1, 3, 6)
group by id_browser
满意归宿 2025-01-13 06:22:13
Try this

SELECT 
   SUM( CASE WHEN id_browser =1 then 1 else 0 ) AS IE
   SUM( CASE WHEN id_browser =2 then 1 else 0 ) AS FIREFOX
   SUM( CASE WHEN id_browser =3 then 1 else 0 ) AS CHROME
   SUM( CASE WHEN id_browser not in (1,2,3) then 1 else 0 ) AS OTHERS
FROM tableName
Try this

SELECT 
   SUM( CASE WHEN id_browser =1 then 1 else 0 ) AS IE
   SUM( CASE WHEN id_browser =2 then 1 else 0 ) AS FIREFOX
   SUM( CASE WHEN id_browser =3 then 1 else 0 ) AS CHROME
   SUM( CASE WHEN id_browser not in (1,2,3) then 1 else 0 ) AS OTHERS
FROM tableName
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文