mysql中in 做條件匹配 帶逗號的字符串 數據異常解決


 

在數據庫中表doctor中cancer字段存放着以逗號分隔的外鍵id

select cancer from doctor where id=1;  

 

數據如下:

cancer
1,2,3




我需要在表cancer_type中匹配剛剛的外鍵,
於是用FIND_IN_SET函數:

select * from cancer_type c where FIND_IN_SET(c.cancer_id, (select cancer from doctor where id=1) )  

數據如下:

cancer_id cancer_name cancer_prefix
1 肺癌 Lung
2 結直腸癌 Col
3 胃癌 Sto







用in來匹配卻不行

select * from cancer_type where cancer_id in (select cancer from doctor where id=1)  


數據如下:

cancer_id cancer_name cancer_prefix
1 肺癌 Lung

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM