[英]PHP/SQL - How to count saturdays in each month?
I have two MySQL tables, called "accounts" and "events".我有两个 MySQL 表,称为“帐户”和“事件”。
Accounts帐户
ID ![]() |
name![]() |
---|---|
1 ![]() |
Pete![]() |
2 ![]() |
Josh![]() |
3 ![]() |
Harry![]() |
Events活动
ID ![]() |
date![]() |
account_id![]() |
---|---|---|
1 ![]() |
2021-10-09 ![]() |
1 ![]() |
2 ![]() |
2021-09-25 ![]() |
1 ![]() |
3 ![]() |
2021-10-23 ![]() |
2 ![]() |
4 ![]() |
2021-11-06 ![]() |
1 ![]() |
5 ![]() |
2021-10-13 ![]() |
1 ![]() |
6 ![]() |
2021-11-17 ![]() |
2 ![]() |
7 ![]() |
2021-11-06 ![]() |
3 ![]() |
8 ![]() |
2021-12-04 ![]() |
3 ![]() |
The account_id in the events table is linked to the id in the accounts table. events 表中的 account_id 链接到 accounts 表中的 id。
My question is: which query can I use to count saturdays in each month (date YYYY-mm-dd format) for each unique user in the accounts table?我的问题是:对于帐户表中的每个唯一用户,我可以使用哪个查询来计算每个月的星期六(日期 YYYY-mm-dd 格式)? So I get the next result:
所以我得到了下一个结果:
Name![]() |
September![]() |
October![]() |
November![]() |
December![]() |
---|---|---|---|---|
Josh![]() |
0 ![]() |
1 ![]() |
0 ![]() |
0 ![]() |
Pete![]() |
1 ![]() |
1 ![]() |
1 ![]() |
0 ![]() |
Harry![]() |
0 ![]() |
0 ![]() |
1 ![]() |
1 ![]() |
I've tried many queries (with ie the (inner) JOIN, DISTINCT and GROUP BY keywords) but I don't get the exact result.我已经尝试了很多查询(即(内部)JOIN、DISTINCT 和 GROUP BY 关键字),但我没有得到确切的结果。 Can you please help me?
你能帮我么?
Many thanks in advance!提前谢谢了!
Use WEEKDAY to tell if a date is Saturday使用WEEKDAY判断日期是否为星期六
Returns the weekday index for date (0 = Monday, 1 = Tuesday, … 6 = Sunday).
返回日期的工作日索引(0 = 星期一,1 = 星期二,... 6 = 星期日)。
SELECT
MAX(a.name) name
, (
SUM( CASE WHEN e.`date` between '2021-09-01' AND '2021-09-30' THEN 1 ELSE 0 END)
) "September"
, (
SUM( CASE WHEN e.`date` between '2021-10-01' AND '2021-10-31' THEN 1 ELSE 0 END)
) "October"
, (
SUM( CASE WHEN e.`date` between '2021-11-01' AND '2021-11-30' THEN 1 ELSE 0 END)
) "November"
, (
SUM( CASE WHEN e.`date` between '2021-12-01' AND '2021-12-31' THEN 1 ELSE 0 END)
) "December"
FROM Accounts a
LEFT JOIN Events e ON e.account_id = a.id AND WEEKDAY(e.`date`) = 5
GROUP BY a.id
;
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=2689e975b18f3a208fdda2d78b50b56c https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=2689e975b18f3a208fdda2d78b50b56c
Basically you can use DAYOFWEEK function and GROUP BY MONTH基本上你可以使用 DAYOFWEEK 函数和 GROUP BY MONTH
SELECT
account_id,
MONTH(date),
COUNT(*)
FROM Events
WHERE DAYOFWEEK(date) = 7
GROUP BY account_id, MONTH(date);
and when you can use PIVOT on received table like:以及何时可以在收到的表上使用 PIVOT,例如:
WITH res AS (
SELECT
account_id,
MONTH(date) mnth,
COUNT(*) cnt
FROM Events
WHERE DAYOFWEEK(date) = 7
GROUP BY account_id, MONTH(date)
) SELECT
account_id,
name,
SUM(mnth=1) Januar,
--
SUM(mnth=9) September,
SUM(mnth=10) October,
SUM(mnth=11) November,
SUM(mnth=12) December
FROM res
JOIN Accounts ON Accounts.id = account_id
GROUP BY account_id, name;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.