1. 程式人生 > >mysql 欄位儲存多個值 ,判斷一個值是否在其中

mysql 欄位儲存多個值 ,判斷一個值是否在其中

表C_file,其中有個欄位是spile,他存的是字元形式,例如:1,2,10,11

C_file

ID     spile
1      2,10,11
2      2,3,20,22
3      1,6,8
4      5,6,1,9
SQL:   select * from C_file where spile LIKE '%1%'
如果這樣查詢的話,會查詢出ID為1、3、4,但正確的應該是3、4

那麼這個SQL語句應該怎麼寫才正確的查詢出1
正確方法:select * from C_file where find_in_set(1,spile)
或者:select * from C_file where instr(concat(',',spile,','),',1,')<>0;