在数据库中表doctor中cancer字段存放着以逗号分隔的外键id
数据如下
[table]
|cancer
|1,2,3
[/table]
我需要在表cancer_type中匹配刚刚的外键,
于是用FIND_IN_SET函数:
数据如下
[table]
|cancer_id|cancer_name|cancer_prefix
|1|肺癌|Lung
|2|结直肠癌|Col
|3|胃癌|Sto
[/table]
用in来匹配却不行
数据如下:
[table]
|cancer_id|cancer_name|cancer_prefix
|1|肺癌|Lung
[/table]
select cancer from doctor where id=1;数据如下
[table]
|cancer
|1,2,3
[/table]
我需要在表cancer_type中匹配刚刚的外键,
于是用FIND_IN_SET函数:
select * from cancer_type c where FIND_IN_SET(c.cancer_id, (select cancer from doctor where id=1) )数据如下
[table]
|cancer_id|cancer_name|cancer_prefix
|1|肺癌|Lung
|2|结直肠癌|Col
|3|胃癌|Sto
[/table]
用in来匹配却不行
select * from cancer_type where cancer_id in (select cancer from doctor where id=1)数据如下:
[table]
|cancer_id|cancer_name|cancer_prefix
|1|肺癌|Lung
[/table]
本文探讨了在数据库中利用FIND_IN_SET函数进行复杂查询的技巧,通过实例展示了如何匹配多个外键,并深入分析了使用in匹配方式的局限性。重点介绍了在不同场景下选择合适的方法以提高数据检索效率。
2506

被折叠的 条评论
为什么被折叠?



