在數據庫中表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 |