2
假設如下:MySQL:如果列包含多個ID,如何使用左連接獲取數據?
表 「信息」:
id | target_ids
----|------------
1 | 2
2 |
3 | 4,1
4 | 2,3,1
表 「目標」:
id | value
----|------------
1 | dog
2 | cat
3 | tiger
4 | lion
使用左加入,我期待這樣的事情:
id | target_ids | value
----|---------------------
1 | 2 | cat
2 | |
3 | 4,1 | lion,dog
4 | 2,3,1 | cat,tiger,dog
我已經嘗試這樣:
select info.*, targets.value from info left join targets on info.target_ids = targets.id
我是在「值」列單值結果
id | target_ids | value
----|---------------------
1 | 2 | cat
2 | |
3 | 4,1 | lion
4 | 2,3,1 | cat
我怎樣才能得到結果,因爲它是顯示在第三表?由於
我會說你的表格格式不正確。你的值不是原子的(見1NF)。無論如何:如果您不能更改表格,您可以從info-table中的數據和您的temp-table中爲聯接創建臨時表格。 – konqi