gpt4 book ai didi

mysql - 计算一周中每一天的出现次数

转载 作者:太空宇宙 更新时间:2023-11-03 12:23:11 24 4
gpt4 key购买 nike

我有这张表:

| name | gender | date             |
------------------------------------
| Foo | male | 2013-09-16 10:23 |
| Name | male | 2013-09-16 09:10 |
| Red | male | 2013-09-15 09:10 |
| Bar | female | 2013-09-15 10:10 |
etc...

我需要获取按性别过滤的一周中每一天的访问次数。

所以我应该得到,如果我算上男性:

1: 2 visits
2: 0 visits
3: 0 visits
4: 0 visits
5: 0 visits
6: 0 visits
7: 1 visit

查询应该是:

SELECT FROM table
WHERE gender = 'male'
GROUP BY DAYOFWEEK

我的查询不起作用,所以我想问问是否有人知道如何让它起作用...

最佳答案

尝试

SELECT dayofweek(date) dayofweek, gender, count(*) count
FROM table
GROUP BY DAYOFWEEK(date), gender

编辑:

如果你想把星期一设为 1,星期日设为 7,你也可以这样做

SELECT (dayofweek(date)+5)%7+1 dayofweek, gender, count(*) count
FROM table
GROUP BY (dayofweek(date)+5)%7+1, gender

如果你想避免 if 结构......

关于mysql - 计算一周中每一天的出现次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18822035/

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