根据另一个字段中的 2 个相同数据分组连接一个字段
group concat a field based on 2 same data in another field
鉴于此 table,
+----+------------------+-----------+----------+
| id | Doctor name | firsttime | lasttime |
+----+------------------+-----------+----------+
| 1 | Dr. Abdurahman | 12:00:00 | 21:00:00 |
| 2 | Dr. Sultan Hamid | 12:00:00 | 21:00:00 |
| 3 | Dr. Susanti | 12:00:00 | 21:00:00 |
| 4 | Dr. Siparman | 12:00:00 | 21:00:00 |
| 5 | Dr. Ramah | 12:00:00 | 21:00:00 |
| 6 | Drs. Susanto | 13:00:00 | 22:00:00 |
| 7 | Dr. Budiarjo | 13:00:00 | 22:00:00 |
| 8 | Dr. Antonius | 13:00:00 | 22:00:00 |
| 9 | Dr. Wahid Bahyu | 13:00:00 | 22:00:00 |
+----+------------------+-----------+----------+
expected
+----+--------------------------------------------+-----------+----------+
| id | Doctor name | firsttime | lasttime |
+----+--------------------------------------------+-----------+----------+
| 1 | Dr. Abdurahman, Dr. Sultan Hamid, etc | 12:00:00 | 21:00:00 |
| 2 | Drs. Susanto, Dr. Budiarjo, etc | 13:00:00 | 22:00:00 |
+----+--------------------------------------------+-----------+----------+
我期待的是 select table 基于第一次和最后一次对 Doctor name 进行组连接。因此,如果 firstdate 和 lastdate 相同,它将包含在组 concat
中
注意:第一个和最后一个日期是随机的,忽略等,我想要医生的全名
使用GROUP_CONCAT
SELECT firstdate,lastdate,
GROUP_CONCAT(doctorname)
FROM
table group by firstdate,lastdate
或者您可以使用 STRING_AGG() 函数:
SELECT STRING_AGG(Doctor name, ', ') AS Doctor name,
firsttime,
lasttime
FROM table
GROUP BY firstdate, lastdate;
鉴于此 table,
+----+------------------+-----------+----------+
| id | Doctor name | firsttime | lasttime |
+----+------------------+-----------+----------+
| 1 | Dr. Abdurahman | 12:00:00 | 21:00:00 |
| 2 | Dr. Sultan Hamid | 12:00:00 | 21:00:00 |
| 3 | Dr. Susanti | 12:00:00 | 21:00:00 |
| 4 | Dr. Siparman | 12:00:00 | 21:00:00 |
| 5 | Dr. Ramah | 12:00:00 | 21:00:00 |
| 6 | Drs. Susanto | 13:00:00 | 22:00:00 |
| 7 | Dr. Budiarjo | 13:00:00 | 22:00:00 |
| 8 | Dr. Antonius | 13:00:00 | 22:00:00 |
| 9 | Dr. Wahid Bahyu | 13:00:00 | 22:00:00 |
+----+------------------+-----------+----------+
expected
+----+--------------------------------------------+-----------+----------+
| id | Doctor name | firsttime | lasttime |
+----+--------------------------------------------+-----------+----------+
| 1 | Dr. Abdurahman, Dr. Sultan Hamid, etc | 12:00:00 | 21:00:00 |
| 2 | Drs. Susanto, Dr. Budiarjo, etc | 13:00:00 | 22:00:00 |
+----+--------------------------------------------+-----------+----------+
我期待的是 select table 基于第一次和最后一次对 Doctor name 进行组连接。因此,如果 firstdate 和 lastdate 相同,它将包含在组 concat
中注意:第一个和最后一个日期是随机的,忽略等,我想要医生的全名
使用GROUP_CONCAT
SELECT firstdate,lastdate,
GROUP_CONCAT(doctorname)
FROM
table group by firstdate,lastdate
或者您可以使用 STRING_AGG() 函数:
SELECT STRING_AGG(Doctor name, ', ') AS Doctor name,
firsttime,
lasttime
FROM table
GROUP BY firstdate, lastdate;