2017-02-21 108 views
0

我需要一些幫助以下查詢。
我有下面的表格,我試圖做一個加入,但預期它不工作:
MySQL左連接多個ID

TABLE1 
Id1 | Title 
1 | A 
2 | B 
3 | C 

TABLE2 
Id2 | Id1 
10 | 1 
20 | 1 
30 | 1,2 

所以基本上對錶2列Id1的可能需要超過1倍的值。 我已經開始用下面的SQL,但它不是按預期工作:

select t1.id1,count(1) 
from table1 t1 
left join table2 
on t1.id1 = t2.id2 
group by t1.id1 desc; 

我也試圖與更換t1.id1 = t2.id2: - t1.id1在t2.id2 - T1 .id1 like concat(「%」,t2.id2,「%」)

但它仍未按預期工作。 該查詢應返回表1中的所有Id,並應計算table2中Id1的所有實例。

任何想法/建議?

+0

*基本上對錶2可以採取多個值列Id1的*,這是根本性的缺陷 – Strawberry

回答

0

以下SQL是返回數據,我需要:

select t1.id1,count(t2.id1) 
    from table1 t1 
    left join table2 t2 
    on find_in_set(t1.id1,t2.id1) 
    group by t1.id1 desc;