mysql查询数据 find_in_set
- 针对数据库字段存储数据为: 1,2,3,4,针对该字段进行模糊查询。
sql查询语句:
select * from 表名 where (find_in_set('1',字段名) or find_in_set('2',字段名));
数据库存储:
针对 tk.mybatis 中Example,查询条件拼接,orCondition()/andCondition() 拼接自定义sql:
Example example = new Example(Test.class);
Example.Criteria criteria = example.createCriteria();
... ...
if (null != labelList && 0 != labelList.size()) {
StringBuilder sql = new StringBuilder();
for (int i = 0; i < labelList.size(); i++) {
if (i == 0) {
sql.append(" ").append("find_in_set('").append(labelList.get(i).getLabelId()).append("',label_id_list)");
} else {
sql.append(" ").append("or find_in_set('").append(labelList.get(i).getLabelId()).append("',label_id_list)");
}
}
criteria.orCondition(sql.toString());
}
List<Test> list = testMapper.selectByExample(example);