gpt4 book ai didi

PHP MySQL : COUNT/SUM a non integer column with different values

转载 作者:行者123 更新时间:2023-11-29 12:55:34 26 4
gpt4 key购买 nike

我的表格如下所示:

ID | click |    time    | browser

1 1 [01:00:00] FireFox
2 1 [01:00:00] FireFox
3 0 [01:00:00] Opera
4 1 [02:00:00] FireFox
5 1 [02:00:00] Chrome
6 1 [02:00:00] Chrome

所以输出应该是:

01:00:00
Total hits: 3
Clicks: 2
FireFox: 2
Opera: 1

02:00:00
Total hits: 3
Clicks: 3
FireFox: 1
Chrome: 2

我知道如何在没有浏览器的情况下获得它:

SELECT SUM(click) AS click, COUNT(*), HOUR(time) 
FROM hits
WHERE time >= DATE_SUB(NOW(),INTERVAL 24 HOUR) GROUP BY HOUR(time)

但我不知道用浏览器输出这个。如果浏览器不被分组并且我得到如下输出,我根本不介意:

01:00:00
Total hits: 3
Clicks: 2

02:00:00
Total hits: 3
Clicks: 3
-----------

FireFox: 3
Chrome: 2
Opera: 1

最佳答案

您可以使用IF:

SELECT SUM(`click`) AS click
, COUNT(*)
, HOUR(`time`)
, COUNT(IF( `browser` = 'Chrome', `browser`, null)) AS `Chrome`
, COUNT(IF( `browser` = 'Opera', `browser`, null)) AS `Opera`
, COUNT(IF( `browser` = 'FireFox', `browser`, null)) AS 'FireFox'
FROM `hits`
WHERE `time` >= DATE_SUB(NOW(),INTERVAL 24 HOUR)
GROUP BY HOUR(`time`)

案例:

SELECT SUM(`click`) AS click
, COUNT(*)
, HOUR(`time`)
, COUNT(CASE WHEN `browser` = 'Chrome' THEN `browser` END) AS `Chrome`
, COUNT(CASE WHEN `browser` = 'Opera' THEN `browser` END) AS `Opera`
, COUNT(CASE WHEN `browser` = 'FireFox' THEN `browser` END) AS 'FireFox'
FROM `hits`
WHERE `time` >= DATE_SUB(NOW(),INTERVAL 24 HOUR)
GROUP BY HOUR(`time`)

关于PHP MySQL : COUNT/SUM a non integer column with different values,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24108199/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com