SELECT tagDeptUserRel.*
FROM tag_dept_user_rel tagDeptUserRel
inner join product prod on find_in_set(prod.id, tagDeptUserRel.dept_mapping_product_ids)
inner join employee creatorEmployee on creatorEmployee.work_no = tagDeptUserRel.creator
inner join employee ownerEmployee on ownerEmployee.work_no = tagDeptUserRel.owner
inner join tag_group tagGroup on find_in_set(tagGroup.id, tagDeptUserRel.tag_group_ids)
inner join tag_group_show_user_rel tagGroupShowUserRel on tagGroupShowUserRel.owner = tagDeptUserRel.owner
where
concat_ws(',',
prod.swork_dept_name,
creatorEmployee.emp_name,
creatorEmployee.nick_name,
ownerEmployee.emp_name,
ownerEmployee.nick_name,
tagGroup.name,
(select tg.name from tag_group tg where tg.id = tagGroupShowUserRel.show_group_id))
like CONCAT('%','之剑','%') ;
SELECT tagDeptUserRel.* ,
concat_ws(',',
prod.swork_dept_name,
creatorEmployee.emp_name,
creatorEmployee.nick_name,
ownerEmployee.emp_name,
ownerEmployee.nick_name,
tagGroup.name,
(select tg.name from tag_group tg where tg.id = tagGroupShowUserRel.show_group_id)) as searchFullText
FROM tag_dept_user_rel tagDeptUserRel
inner join product prod on find_in_set(prod.id, tagDeptUserRel.dept_mapping_product_ids)
inner join employee creatorEmployee on creatorEmployee.work_no = tagDeptUserRel.creator
inner join employee ownerEmployee on ownerEmployee.work_no = tagDeptUserRel.owner
inner join tag_group tagGroup on find_in_set(tagGroup.id, tagDeptUserRel.tag_group_ids)
inner join tag_group_show_user_rel tagGroupShowUserRel on tagGroupShowUserRel.owner = tagDeptUserRel.owner
where
concat_ws(',',
prod.swork_dept_name,
creatorEmployee.emp_name,
creatorEmployee.nick_name,
ownerEmployee.emp_name,
ownerEmployee.nick_name,
tagGroup.name,
(select tg.name from tag_group tg where tg.id = tagGroupShowUserRel.show_group_id))
like CONCAT('%','之剑','%') ;
在mysql中,有时我们在做数据库查询时,需要得到某字段中包含某个值的记录,但是它也不是用like能解决的,使用like可能查到我们不想要的记录,它比like更精准,这时候mysql的FIND_IN_SET函数就派上用场了,下面来具体了解一下。
FIND_IN_SET(str,strlist)函数
str 要查询的字符串
strlist 字段名 参数以”,”分隔 如 (1,2,6,8)
查询字段(strlist)中包含(str)的结果,返回结果为null或记录
下面举例说明
test表中有如下字段及值
下面我想查询area中包含”1″这个参数的记录
SELECT * from test where FIND_IN_SET('1',area) 返回值
下面查询btype字段中包含”15″这个参数的值
SELECT * from test where FIND_IN_SET('15',btype) 返回值
下面查询btype字段中包含”5″这个参数的值
SELECT * from test where FIND_IN_SET('5',btype) 返回值为null,因为btype中没有”5”这个值,它不同于 like 模糊查询,它是以“,”来分隔值
接下面查询btype字段中包含”20″这个参数的值
SELECT * from test where FIND_IN_SET('20',btype) 当然它的返回值为null,因为字段中没有这个值
FIND_IN_SET和like的区别
like是广泛的模糊匹配,字符串中没有分隔符,Find_IN_SET 是精确匹配,字段值以英文”,”分隔,Find_IN_SET查询的结果要小于like查询的结果。
SELECT tagDeptUserRel.* ,
concat_ws(',',
prod.swork_dept_name,
creatorEmployee.emp_name,
creatorEmployee.nick_name,
ownerEmployee.emp_name,
ownerEmployee.nick_name,
tagGroup.name,
(select tg.name from tag_group tg where tg.id = tagGroupShowUserRel.show_group_id)) as searchFullText
FROM tag_dept_user_rel tagDeptUserRel
...
where
concat_ws(',',
prod.swork_dept_name,
creatorEmployee.emp_name,
creatorEmployee.nick_name,
ownerEmployee.emp_name,
ownerEmployee.nick_name,
tagGroup.name,
(select tg.name from tag_group tg where tg.id = tagGroupShowUserRel.show_group_id))
like CONCAT('%','之剑','%')