gpt4 book ai didi

Mysql 统计每个月的浏览者总数

转载 作者:行者123 更新时间:2023-11-29 06:55:07 24 4
gpt4 key购买 nike

我打算每个月从数据库中统计每个观众。我尝试了很多方法,但我没有运气。这是代码示例

SELECT MONTH_v, YEAR_V, 
SUM(IF(day_v=1, views, 0)) AS '1',
SUM(IF(day_v=2, views, 0)) AS '2',
SUM(IF(day_v=3, views, 0)) AS '3',
SUM(IF(day_v=4, views, 0)) AS '4',
SUM(IF(day_v=5, views, 0)) AS '5',
SUM(IF(day_v=6, views, 0)) AS '6',
SUM(IF(day_v=7, views, 0)) AS '7',
SUM(IF(day_v=8, views, 0)) AS '8',
SUM(IF(day_v=9, views, 0)) AS '9',
SUM(IF(day_v=10, views, 0)) AS '10',
SUM(IF(day_v=11, views, 0)) AS '11',
SUM(IF(day_v=12, views, 0)) AS '12',
SUM(IF(day_v=13, views, 0)) AS '13',
SUM(IF(day_v=14, views, 0)) AS '14',
SUM(IF(day_v=15, views, 0)) AS '15',
SUM(IF(day_v=16, views, 0)) AS '16',
SUM(IF(day_v=17, views, 0)) AS '17',
SUM(IF(day_v=18, views, 0)) AS '18',
SUM(IF(day_v=19, views, 0)) AS '19',
SUM(IF(day_v=20, views, 0)) AS '20',
SUM(IF(day_v=21, views, 0)) AS '21',
SUM(IF(day_v=22, views, 0)) AS '22',
SUM(IF(day_v=23, views, 0)) AS '23',
SUM(IF(day_v=24, views, 0)) AS '24',
SUM(IF(day_v=25, views, 0)) AS '25',
SUM(IF(day_v=26, views, 0)) AS '26',
SUM(IF(day_v=27, views, 0)) AS '27',
SUM(IF(day_v=28, views, 0)) AS '28',
SUM(IF(day_v=29, views, 0)) AS '29',
SUM(IF(day_v=30, views, 0)) AS '30',
SUM(IF(day_v=31, views, 0)) AS '31'
FROM
(
SELECT DAY(vwr_date) AS day_v,
MONTH(vwr_date) AS MONTH_v,
Year(vwr_date) AS YEAR_V,
date(vwr_date) AS date_v,
count(vwr_id) AS views
FROM car_viewer
WHERE Year(vwr_date)='2012' AND vwr_tid='18'
GROUP BY date_v
) as viewz
GROUP BY MONTH_v, YEAR_V
ORDER BY MONTH_v, YEAR_V DESC;

现场直播在这里:http://www.sqlfiddle.com/#!2/75aa8/1

问题是我想在每行的右边添加两列:

  1. 总计 = 每个查看者,包括每行的 0 个。
  2. 平均 = 每行的平均观看者。

请不要犹豫,助我一臂之力。 :)

引用:MySQL count(*) everyday in a month returns [BLOB-2B] instead of number

图片来源:Ross Smith II

最佳答案

只需在外部查询中添加SUM(views):

SELECT MONTH_v, YEAR_V, 

...

SUM(IF(day_v=30, views, 0)) AS '30',
SUM(IF(day_v=31, views, 0)) AS '31',
SUM(views)
FROM
(
SELECT DAY(vwr_date) AS day_v,

....

Updated SQL Fiddle

关于Mysql 统计每个月的浏览者总数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13216048/

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