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

作者: 光剑书架上的书 | 来源:发表于2017-08-17 01:47 被阅读196次

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

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

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('%','之剑','%')

相关文章

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

    mysql 多表关联查询 实现 全文匹配的 模糊搜索接口 SQL mysql中FIND_IN_SET的使用方法 ...

  • mysql实现多表关联

    mysql实现多表关联 mysql关联单张表

  • MySQL 多表关联同步到 ES 的实践

    背景 线上问题:业务系统查询,涉及多表关联查询,条件维度较大且有模糊匹配需求,索引无法覆盖,导致查询性能较低。 解...

  • Elasticsearch Query DSL之全文检索(Ful

    本节将详细介绍DSL全文搜索。全文查询包括如下几种模式: match query标准的全文检索模式,包含模糊匹配、...

  • MySQL的全文搜索

    MySQL支持全文搜索,主要是解决Like查询或其他多条件模糊查询带来的性能问题 以下文章内容主要介绍如何使用全文...

  • python面试题01

    1、什么是多表关联查询,有几种多表关联的查询方式,分别是什么? 多表关联查询概念: 多表关联查询分类:1.1内连接...

  • sphinx(二)centos7编译安装coreseek3.2

    文章搜索我现在使用的是mysql的模糊查询like搜索标题关键字。 之前也有用全文索引,但是全文索引的效率比较低,...

  • FULLTEXT 搜索

    FULLTEXT 搜索 MySQL 的全文搜索功能,不使用模板匹配操作的情况下,进行单词或短语的查找。 全文搜索 ...

  • MySQL的多表关联查询

    一、多表关联查询 多表关联查询是使用一条SQL语句,将关联的多张表的数据查询出来。 1.1 交叉查询 交叉查询就是...

  • sql查询数值为空,展示为0

    多表联查时,查一个关联数值,没有关联记录,查询结果为null,但要求处理为0。 mysql: 原来要查询的字段:a...

网友评论

    本文标题:mysql 多表关联查询 实现 全文匹配的 模糊搜索接口 SQ

    本文链接:https://www.haomeiwen.com/subject/bbtgrxtx.html