mysql - 如何在 MySQL 中正确分组
问题描述
我在 mysql 中遇到 group by 问题。以下是从我的测试表中获取所有事件/描述的示例查询/结果:
SELECT
customer.id AS id,
customer.first_name AS firstName,
customer.middle_names AS middleNames,
customer.last_name AS lastName,
office.handler AS handler,
office.handled AS handled,
event.day AS eventDay,
reason.description,
FROM event event
LEFT JOIN alert alert on event.id = alert.event_id
LEFT JOIN reason reason on alert.id = reason.alert_id
LEFT JOIN customer on customer.id = alert.customer_id
LEFT JOIN (
SELECT identity, handler, handled, MAX(created) AS created
FROM office_handling GROUP BY identity
) office ON (customer.identity_id = handling.identity)
ORDER BY eventDay DESC
+----------+-----------+-------------+----------+---------+---------+---------------------+-------------------
| id | firstName | middleNames | lastName | handler | handled | eventDay | description |
+----------+-----------+-------------+----------+---------+---------+---------------------+-------------------
| 137 | Test2 | Somebody | Last2 | NULL | NULL | 2021-06-25 00:00:00 | Some description |
| 137 | Test2 | Somebody | Last2 | NULL | NULL | 2021-06-25 00:00:00 | Some description |
| 136 | Test1 | Someone | Last1 | NULL | NULL | 2021-06-25 00:00:00 | Some description |
| 136 | Test1 | Someone | Last1 | NULL | NULL | 2021-06-25 00:00:00 | Some description |
| 137 | Test2 | Somebody | Last2 | NULL | NULL | 2021-04-15 00:00:00 | Some description |
| 137 | Test2 | Somebody | Last2 | NULL | NULL | 2021-04-15 00:00:00 | Some description |
| 136 | Test1 | Someone | Last1 | NULL | NULL | 2021-04-15 00:00:00 | Some description |
| 136 | Test1 | Someone | Last1 | NULL | NULL | 2021-04-15 00:00:00 | Some description |
+----------+-----------+-------------+----------+---------+---------+---------------------+-------------------
将此查询与 group by 一起使用时:
SELECT
customer.id AS id,
customer.first_name AS firstName,
customer.middle_names AS middleNames,
customer.last_name AS lastName,
office.handler AS handler,
office.handled AS handled,
event.day AS eventDay,
COUNT(reason.description) as count,
FROM event event
LEFT JOIN alert alert on event.id = alert.event_id
LEFT JOIN reason reason on alert.id = reason.alert_id
LEFT JOIN customer on customer.id = alert.customer_id
LEFT JOIN (
SELECT identity, handler, handled, MAX(created) AS created
FROM office_handling GROUP BY identity
) office ON (customer.identity_id = handling.identity)
GROUP BY id ORDER BY eventDay DESC
结果我得到:
+----------+-----------+-------------+----------+---------+---------+---------------------+--------
| id | firstName | middleNames | lastName | handler | handled | eventDay | count |
+----------+-----------+-------------+----------+---------+---------+---------------------+--------
| 136 | Test1 | Someone | Last1 | NULL | NULL | 2021-06-25 00:00:00 | 8 |
+----------+-----------+-------------+----------+---------+---------+---------------------+--------
我想要得到的是:
+----------+-----------+-------------+----------+---------+---------+---------------------+--------
| id | firstName | middleNames | lastName | handler | handled | eventDay | count |
+----------+-----------+-------------+----------+---------+---------+---------------------+--------
| 136 | Test1 | Someone | Last1 | NULL | NULL | 2021-06-25 00:00:00 | 4 |
+----------+-----------+-------------+----------+---------+---------+---------------------+--------
| 137 | Test2 | Somebody | Last2 | NULL | NULL | 2021-06-25 00:00:00 | 4 |
+----------+-----------+-------------+----------+---------+---------+---------------------+--------
我只是似乎无法正确修改查询以获得想要的结果。任何帮助如何做到这一点?
解决方案
推荐阅读
- microsoft-graph-api - 页面的 onenote 级别和顺序属性
- angular - 组验证器不会影响 form.isValid
- ios - 核心数据。如何交换 NSPersistentStores 并通知 NSFetchedResultsController?
- python - 如何预期 Python 中的机器人框架会失败?
- java - 日期时间比较给出不正确的结果
- python - 使用 isin() 从文本文件导入列表以与数据框进行比较
- angular - 如何使用模块在 Angular 6 中指定多个组件
- vaadin - pwa 工作箱在缓存中找不到响应
- php - php-尝试在引用时修复意外的“:”
- macros - nom 如何在宏指示符位置进行宏调用?