<分区>
我有两个表:
订单
+----+------+-------+
| id | name | notes |
+----+------+-------+
| 1 | Adam | 1,2 |
| 2 | Ema | 3 |
| 3 | Petr | 1,3 |
+----+------+-------+
注意事项
+----+---------------------+
| id | text |
+----+---------------------+
| 1 | This is first note |
| 2 | This is second note |
| 3 | And third note |
+----+---------------------+
我需要从 Orders 中选择行,并根据 Orders.notes 从第二个表中对 concat Text 进行分组。
如果我使用这个语句
SELECT o.name, o.notes
,GROUP_CONCAT(DISTINCT n.text SEPARATOR ';') AS notes_text
FROM orders o
LEFT JOIN notes n ON n.id IN (1,2)
WHERE o.id = 1;
结果符合预期This is first note;This is second note
但是如果我使用我需要的这条语句,where notes.id IN (orders.notes)
SELECT o.name, o.notes
,GROUP_CONCAT(DISTINCT n.text SEPARATOR ';') AS notes_text
FROM orders o
LEFT JOIN notes n ON n.id IN (o.notes)
WHERE o.id = 1
它只返回第一个文本This is first note
。为什么?