如何在 MySQL 中使用 group by 和 pivot?
How to use group by with pivot in MySQL?
任何人都可以帮我分组 2 tables 并显示如下...
我的第一个 table 是:
user_id |部
----------------------
1 |用户1
----------------------
2 |用户2
----------------------
和 table 2 是:
reserve_id | user_id | reserve_status
--------------------------------------
1 | 1 | 1个
--------------------------------------
2 | 1 | 1个
--------------------------------------
3 | 1 | 2个
--------------------------------------
4 | 2 | 2个
--------------------------------------
5 | 2 | 0
--------------------------------------
6 | 2 | 1个
*reserve_status 0 取消选中,1 = 批准,2 = 不批准
我想要这样的产品:
部门|总和储备 | sumreservapprove | sumreserveunapprove |取消勾选
---------------------------------------------- --------------------------
用户1 | 3 | 2 | 1 | 0
---------------------------------------------- --------------------------
用户2 | 3 | 1 | 1 | 1个
抱歉英语不好
您需要一个枢纽解决方案。
演示:
假设 so_q27895250_users
和 so_q27895250_reserve
作为 table 名称
mysql>
mysql> select u.department, count(r.reserve_status) as total_reserves,
-> count( case when r.reserve_status = 0
-> then r.reserve_status else null end ) as unchecked,
-> count( case when r.reserve_status = 1
-> then r.reserve_status else null end ) as approved,
-> count( case when r.reserve_status = 2
-> then r.reserve_status else null end ) as unapproved
-> from so_q27895250_reserve r
-> left join so_q27895250_users u
-> on r.user_id = u.user_id
-> group by r.user_id
-> ;
+------------+----------------+-----------+----------+------------+
| department | total_reserves | unchecked | approved | unapproved |
+------------+----------------+-----------+----------+------------+
| user1 | 3 | 0 | 2 | 1 |
| user2 | 3 | 1 | 1 | 1 |
+------------+----------------+-----------+----------+------------+
2 rows in set (0.00 sec)
任何人都可以帮我分组 2 tables 并显示如下...
我的第一个 table 是:
user_id |部 ---------------------- 1 |用户1 ---------------------- 2 |用户2 ----------------------
和 table 2 是:
reserve_id | user_id | reserve_status -------------------------------------- 1 | 1 | 1个 -------------------------------------- 2 | 1 | 1个 -------------------------------------- 3 | 1 | 2个 -------------------------------------- 4 | 2 | 2个 -------------------------------------- 5 | 2 | 0 -------------------------------------- 6 | 2 | 1个 *reserve_status 0 取消选中,1 = 批准,2 = 不批准
我想要这样的产品:
部门|总和储备 | sumreservapprove | sumreserveunapprove |取消勾选 ---------------------------------------------- -------------------------- 用户1 | 3 | 2 | 1 | 0 ---------------------------------------------- -------------------------- 用户2 | 3 | 1 | 1 | 1个
抱歉英语不好
您需要一个枢纽解决方案。
演示:
假设 so_q27895250_users
和 so_q27895250_reserve
作为 table 名称
mysql>
mysql> select u.department, count(r.reserve_status) as total_reserves,
-> count( case when r.reserve_status = 0
-> then r.reserve_status else null end ) as unchecked,
-> count( case when r.reserve_status = 1
-> then r.reserve_status else null end ) as approved,
-> count( case when r.reserve_status = 2
-> then r.reserve_status else null end ) as unapproved
-> from so_q27895250_reserve r
-> left join so_q27895250_users u
-> on r.user_id = u.user_id
-> group by r.user_id
-> ;
+------------+----------------+-----------+----------+------------+
| department | total_reserves | unchecked | approved | unapproved |
+------------+----------------+-----------+----------+------------+
| user1 | 3 | 0 | 2 | 1 |
| user2 | 3 | 1 | 1 | 1 |
+------------+----------------+-----------+----------+------------+
2 rows in set (0.00 sec)