联合同一张表中的同一列

时间:2021-02-17 06:01:23

标签: mysql sql

我有一张这样的桌子

ID | booking_id | branch_id | service_id | staff_id | rating
1  |    21      |    2      |      null  |   null   |   5
2  |    21      |    null   |      5     |   null   |   3
3  |    21      |    null   |      7     |   null   |   5
4  |    21      |    null   |      8     |   null   |   5
5  |    21      |    null   |    null    |    7     |   5
6  |    22      |    3      |    null    |   null   |   4
7  |    22      |    null   |      8     |   null   |   2
8  |    22      |    null   |    null    |    10    |   1

booking_id相同时我要返回的就是这样

booking_id |branch name  |service name           | staff name |branch rating|service rating|staff rating 
    21     |LA branch    |massage, wax, therapy  |   John     |  5          |  3 , 5 ,5    | 5
    22     |Vegas Branch |   therapy               |   May    |  4          |      2       | 1

服务 ID 只会与评级中的服务评级相同的列合并。

1 个答案:

答案 0 :(得分:0)

对于 service_rating,您应该使用 group_concat() 而不是 MAX()

 group_concat(service_id)) AS service_rating,