问题描述:
想要查询表A中 包含主键id(int类型)的所有数据,前端传来参数为id的拼接字符串,例如:1,2,3,4。
错误记录:
select * from A where id in (?1)
其中?1,为前端传来的参数:1,2,3,4,最终查询结果为null,无记录
错误分析:
本意想查询的应是
select * from A where id in (0,2,1,3)
查询中其实是:
select * from Rong_App_Cart where id in ('0,2,1,3')
解决方法:
使用 FIND_IN_SET 函数
select * from A where FIND_IN_SET(id,'0,1,2,3') -- sql1
select * from A where FIND_IN_SET(id,'0','1','2','3') -- sql2
select * from A where FIND_IN_SET(id,?1)
题外话:sql1 与 sql2 的性能是不一样的