在条件下对行进行分组,获取所有结果

时间:2018-02-05 07:46:11

标签: mysql database relational-database

我正在开发类似facebook的通知系统。当实体ID和操作类型(标记为CUD)相同时,我想分组数据。所以从这个:

+----+-----------------+--------+------------+--------+-----------+---------------------+---------------------+---------------------+-----+---------------+-----------+
| id | display_name    | name   | surname    | avatar | type      | last_check          | redirect            | created             | cud | notif_culprit | entity_id |
+----+-----------------+--------+------------+--------+-----------+---------------------+---------------------+---------------------+-----+---------------+-----------+
| 45 |                 | Julie  | Opletalová |        | character | Character:edit, 316 | Character:edit, 316 | 2018-02-03 00:55:04 |   2 |             5 |       316 |
| 52 | juliina postava | Julie  | Opletalová |        | character | Character:edit, 325 | Character:edit, 325 | 2018-02-03 01:22:47 |   1 |             5 |       325 |
| 62 | veveho postava  | Martin | Nezamysl   |        | character | Character:edit, 326 | Character:edit, 326 | 2018-02-03 23:15:39 |   2 |             3 |       326 |
| 62 | veveho postava  | Martin | Nezamysl   |        | character | Character:edit, 326 | Character:edit, 326 | 2018-02-03 23:51:39 |   3 |             3 |       326 |
| 62 | veveho postava  | Martin | Nezamysl   |        | character | Character:edit, 326 | Character:edit, 326 | 2018-02-03 23:57:23 |   3 |             3 |       326 |
| 62 | veveho postava  | Martin | Nezamysl   |        | character | Character:edit, 326 | Character:edit, 326 | 2018-02-04 00:05:45 |   3 |             3 |       326 |
| 62 | veveho postava  | Martin | Nezamysl   |        | character | Character:edit, 326 | Character:edit, 326 | 2018-02-04 00:06:02 |   3 |             3 |       326 |
| 62 | veveho postava  | Martin | Nezamysl   |        | character | Character:edit, 326 | Character:edit, 326 | 2018-02-04 00:06:16 |   3 |             3 |       326 |
| 62 | veveho postava  | Martin | Nezamysl   |        | character | Character:edit, 326 | Character:edit, 326 | 2018-02-04 00:23:50 |   3 |             3 |       326 |
| 62 | veveho postava  | Martin | Nezamysl   |        | character | Character:edit, 326 | Character:edit, 326 | 2018-02-04 00:30:06 |   3 |             3 |       326 |
| 62 | veveho postava  | Martin | Nezamysl   |        | character | Character:edit, 326 | Character:edit, 326 | 2018-02-04 00:30:36 |   3 |             3 |       326 |
+----+-----------------+--------+------------+--------+-----------+---------------------+---------------------+---------------------+-----+---------------+-----------+

我想

+----+-----------------+--------+------------+--------+-----------+---------------------+---------------------+---------------------+-----+---------------+-----------+
| id | display_name    | name   | surname    | avatar | type      | last_check          | redirect            | created             | cud | notif_culprit | entity_id |
+----+-----------------+--------+------------+--------+-----------+---------------------+---------------------+---------------------+-----+---------------+-----------+
| 45 |                 | Julie  | Opletalová |        | character | Character:edit, 316 | Character:edit, 316 | 2018-02-03 00:55:04 |   2 |             5 |       316 |
| 52 | juliina postava | Julie  | Opletalová |        | character | Character:edit, 325 | Character:edit, 325 | 2018-02-03 01:22:47 |   1 |             5 |       325 |
| 62 | veveho postava  | Martin | Nezamysl   |        | character | Character:edit, 326 | Character:edit, 326 | 2018-02-03 23:15:39 |   2 |             3 |       326 |
| 62 | veveho postava  | Martin | Nezamysl   |        | character | Character:edit, 326 | Character:edit, 326 | 2018-02-03 23:51:39 |   3 |             3 |       326 |

我知道"有"在mysql中命令,所以我可以使用group by指定类型。但我只用一种类型(CUD)就可以做到这一点。我需要显示所有类型。

这是SqlFiddle: http://sqlfiddle.com/#!9/5a8361/1

1 个答案:

答案 0 :(得分:1)

您必须按ID和cud对结果进行分组。

SELECT * FROM mytable
GROUP BY id, cud;

SQL FIDDLE DEMO