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;
本文探讨了MySQL中四种解决模糊查询包含关系的方法,包括使用LIKE、find_in_set()、locate()和INSTR()函数,旨在帮助开发者精准筛选数据,避免因包含关系导致的查询错误。

3万+

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



