前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >mysql 多表关联查询 实现 全文匹配的 模糊搜索接口 SQLmysql 多表关联查询 实现 全文匹配的 模糊搜索接口 SQL

mysql 多表关联查询 实现 全文匹配的 模糊搜索接口 SQLmysql 多表关联查询 实现 全文匹配的 模糊搜索接口 SQL

作者头像
一个会写诗的程序员
发布2018-08-17 15:06:06
2.4K0
发布2018-08-17 15:06:06
举报

mysql 多表关联查询 实现 全文匹配的 模糊搜索接口 SQL

代码语言:javascript
复制
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('%','之剑','%') ;
代码语言:javascript
复制
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中FIND_IN_SET的使用方法

在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 嵌套使用

代码语言:javascript
复制
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('%','之剑','%')
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017.08.17 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • mysql 多表关联查询 实现 全文匹配的 模糊搜索接口 SQL
    • mysql中FIND_IN_SET的使用方法
      • select 嵌套使用
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档