2012-03-30 92 views
2

表1如何將值與MySQL中的csv值進行比較?

id(int) | name(varchar) 
1  | at,bat 
2  | cat,at,bat,mat 
3  | mat,cat 
4  | sat,bat 

表2

id(int) | type(varchar) 
1  | at 
2  | mat 

正如你可以看到表1中包含CSV字符串。現在我需要從Table 1中獲取id s,其name s存在於Table2類型字段中。

有沒有這樣做的純粹的MySQL查詢方式?如果不是,那麼在大型記錄集的情況下,這樣做最省時的方式是什麼?

+2

只是要確定,你要取消CONCAT的''每個name' table1'到行並做一個'where table1.name = table2.type'? – cctan 2012-03-30 07:09:45

回答

2

here

select a.ids as id1, b.ids as id2, a.name,b.type 
from table1 a 
inner join table2 b on find_in_set(b.type,a.name)