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

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: mysql 多表关联查询 实现 全文匹配的 模糊搜索接口 SQLSELECT tagDeptUserRel.* FROM tag_dept_user_rel tagDeptUserRelinner join product prod on find_in_set(prod.

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

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
14天前
|
SQL
sql语句加正则 简化查询
sql语句加正则 简化查询
14 0
sql语句加正则 简化查询
|
1月前
|
SQL
sql server链接查询
sql server链接查询
17 1
|
1月前
|
SQL
sql server简单查询
sql server简单查询
14 1
|
22天前
|
SQL 关系型数据库 MySQL
mysql一条sql查询出多个统计结果
mysql一条sql查询出多个统计结果
14 0
|
9天前
|
SQL 关系型数据库 数据库
【后端面经】【数据库与MySQL】SQL优化:如何发现SQL中的问题?
【4月更文挑战第12天】数据库优化涉及硬件升级、操作系统调整、服务器/引擎优化和SQL优化。SQL优化目标是减少磁盘IO和内存/CPU消耗。`EXPLAIN`命令用于检查SQL执行计划,关注`type`、`possible_keys`、`key`、`rows`和`filtered`字段。设计索引时考虑外键、频繁出现在`where`、`order by`和关联查询中的列,以及区分度高的列。大数据表改结构需谨慎,可能需要停机、低峰期变更或新建表。面试中应准备SQL优化案例,如覆盖索引、优化`order by`、`count`和索引提示。优化分页查询时避免大偏移量,可利用上一批的最大ID进行限制。
34 3
|
15天前
|
SQL 存储 关系型数据库
【MySQL实战笔记】02.一条SQL更新语句是如何执行的-2
【4月更文挑战第5天】两阶段提交是为确保`redo log`和`binlog`逻辑一致,避免数据不一致。若先写`redo log`, crash后数据可能丢失,导致恢复后状态错误;若先写`binlog`,crash则可能导致重复事务,影响数据库一致性。一天一备相较于一周一备,能缩短“最长恢复时间”,但需权衡额外的存储成本。
16 1
|
22天前
|
SQL 关系型数据库 MySQL
【MySQL】慢SQL分析流程
【4月更文挑战第1天】【MySQL】慢SQL分析流程
|
26天前
|
SQL 关系型数据库 MySQL
【MySQL技术之旅】(7)总结和盘点优化方案系列之常用SQL的优化
【MySQL技术之旅】(7)总结和盘点优化方案系列之常用SQL的优化
39 1
|
27天前
|
SQL 关系型数据库 MySQL
MySQL SQL语句面试准备
MySQL SQL语句面试准备
12 0
|
16天前
|
关系型数据库 MySQL 数据库
mysql卸载、下载、安装(window版本)
mysql卸载、下载、安装(window版本)