mysql解决模糊查询包含关系

mysql解决模糊查询包含关系

后台要根据期限筛选查询时如果用like,

SELECT * from t_user_accord_invest t where t.invest_period like '%1%';

就会出现 参数为1时 ,13的也能筛选出来,出现查询bug。

解决方案

利用mysql 字符串函数 find_in_set();

SELECT * from t_user_accord_invest t where find_in_set(1,t.invest_period);

完美避免like出现的问题。

mysql查询包含 4种方法

方法一:like

SELECT * from t_user_accord_invest t where t.invest_period like '%1%';

方法二:find_in_set(字符, 字段名)

SELECT * from t_user_accord_invest t where find_in_set(1,t.invest_period);

方法三:locate(字符,字段名)

SELECT * from t_user_accord_invest t where locate(1,t.invest_period) and t.is_use=1;

方法四 INSTR(字段名,字符)

SELECT * from t_user_accord_invest t where INSTR(t.invest_period,1) and t.is_use=1;

猜你喜欢

转载自blog.csdn.net/qq_33243164/article/details/89531558