我在數據庫中有2表:mainTable和classificationTable:MySQL的選擇與子查詢
mainTable:
id | classification_id | name
---------------------------------------
1 | 1 | Name1
2 | 2,3,4 | Name2
3 | 1,4 | Name3
4 | 4 | Name4
classificationTable:
classification_id | class_name
---------------------------------------
1 | Class Name1
2 | Class Name2
3 | Class Name3
4 | Class Name4
我希望得到一個選擇,例如用於來自mainTable的ID 3的行,如:
id = 3
class_name = Class Name1, Class Name4
Name = Name3
我嘗試此選擇,但是這僅返回首先從陣列elemnt(前exempla用於行與ID 3,該返回唯一的類名1)
SELECT i.*,
(SELECT GROUP_CONCAT(cl.class_name) FROM classificationTable as cl WHERE cl.classification_id IN(i.classification_id)) as class_name
FROM mainTable as i;
幫助PLZ。
哪裏是classification_name在你的桌子嗎? –
對不起,是我的錯。是class_name –