我有两个表:MySQL的:LEFT JOIN表ON table.id IN()
订单
+----+------+-------+
| 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 |
+----+---------------------+
我需要选择行来自订单和组concat基于Orders.notes的第二个表中的文本。
如果我用这个语句如预期This is first note;This is second note
但
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;
的结果,如果我用这个说法,我需要,其中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
。为什么?
你只是问'为什么'或者你需要另一个想要的结果? – RubahMalam
我需要使用第二条语句,但期望得到类似于第一条语句的结果。 – hovado
'orders.notes'是这个问题的一个不可扩展的解决方案。你应该把'order_id'放到你的'notes'表中,那么SQL就会更有意义。 –