项目中遇到模糊查询,总结了几种查询方法,当然只是暂时用一下,后期要改为全文检索
1. sql中字符串拼接
SELECT * FROM tableName WHERE name LIKE CONCAT(CONCAT('%', #{text}), '%');
2. 使用 ${...} 代替 #{...}
SELECT * FROM tableName WHERE name LIKE '%${text}%';
3. 程序中拼接
Java
// or String searchText = "%" + text + "%";
String searchText = new StringBuilder("%").append(text).append("%").toString();
parameterMap.put("text", searchText);
SqlMap.xml
SELECT * FROM tableName WHERE name LIKE #{text};
4. 大小写匹配查询
[sql] view plain copy
SELECT * FROM TABLENAME WHERE UPPER(SUBSYSTEM) LIKE '%' || UPPER('jz') || '%'
--或者是
1. sql中字符串拼接
SELECT * FROM tableName WHERE name LIKE CONCAT(CONCAT('%', #{text}), '%');
2. 使用 ${...} 代替 #{...}
SELECT * FROM tableName WHERE name LIKE '%${text}%';
3. 程序中拼接
Java
// or String searchText = "%" + text + "%";
String searchText = new StringBuilder("%").append(text).append("%").toString();
parameterMap.put("text", searchText);
SqlMap.xml
SELECT * FROM tableName WHERE name LIKE #{text};
4. 大小写匹配查询
[sql] view plain copy
SELECT * FROM TABLENAME WHERE UPPER(SUBSYSTEM) LIKE '%' || UPPER('jz') || '%'
--或者是
SELECT * FROM TABLENAME WHERE LOWER(SUBSYSTEM) LIKE '%' || LOWER('jz') || '%'
前两种方式可用,后两种没有来得及测试,大家可以试试,或提出更好的方式