当前位置:网站首页>Mysql多对多关系,分组拼接把多个数据查询到一条数据上
Mysql多对多关系,分组拼接把多个数据查询到一条数据上
2022-07-29 05:06:00 【nianyuw】
- GROUP_CONCAT(str):分组字符串拼接,与分组一起使用
案例:查询企业信息以及企业分类信息,其中企业分类信息和企业是多对多的关系,按普通的联表查询,我们会查询到一条企业信息对应多个企业分类,会出现多个记录。如果想实现把同一个企业的不同企业分类一个记录表示,就可以考虑使用GROUP_CONCAT,该函数会把多条数据拼接在一条数据上(默认使用,分割)
直接联表查询,会看到一个企业关联的企业分类是多个数据
select t_enterprise.*,
dic_enterprise_category.name as enterpriseCategoryNames,
dic_operation_status.name as operationStatusName,
dic_region.name as regionName
from t_enterprise
left join ref_enterprise_category
on t_enterprise.id = ref_enterprise_category.enterprise_id
left join dic_enterprise_category
on dic_enterprise_category.id = ref_enterprise_category.category_id
left join dic_operation_status
on t_enterprise.operate_status_id = dic_operation_status.id
left join dic_region
on t_enterprise.region_id = dic_region.id
使用GROUP_CONCAT
select
t_enterprise.*,
GROUP_CONCAT(DISTINCT dic_enterprise_category.name) as enterpriseCategoryNames,
dic_operation_status.name as operationStatusName,
dic_region.name as regionName
from t_enterprise
left join ref_enterprise_category
on t_enterprise.id = ref_enterprise_category.enterprise_id
left join dic_enterprise_category
on dic_enterprise_category.id = ref_enterprise_category.category_id
left join dic_operation_status
on t_enterprise.operate_status_id = dic_operation_status.id
left join dic_region
on t_enterprise.region_id = dic_region.id
GROUP BY id
```=

-
边栏推荐
- Exception - ...MaxUploadSizeExceededException: Maximum upload size exceeded; nested exception is ...
- How to add traffic statistics codes to the legendary Development Zone website
- How to add a map to the legendary server
- 深度学习刷SOTA的一堆trick
- 那个准时上下班,从不愿意加班加点的人,在我前面升职了...
- The method and detailed code of automatically pop-up and QQ group when players visit the website
- 荣耀2023内推,内推码ambubk
- Pivot table of odoo development tutorial
- Use openmap and ArcGIS to draw maps and transportation networks of any region, and convert OMS data into SHP format
- Excel卡住了没保存怎么办?Excel还没保存但是卡住了的解决方法
猜你喜欢
【文件下载】Easyexcel快速上手
IDEA中使用注解Test
[2022 freshmen learning] key points of the third week
How does word view document modification traces? How word views document modification traces
让你的正则表达式可读性提高一百倍
[file download] easyexcel quick start
EMI interference troubleshooting with near-field probe and current probe
C language implementation of three chess
新产品上市最全推广方案
How to install Office2010 installation package? How to install Office2010 installation package on computer
随机推荐
MySQL time calculation function
Download addresses of various versions of MySQL and multi version coexistence installation
玩家访问网站自动弹窗加QQ群方法以及详细代码
Live in small private enterprises
虚拟偶像的歌声原来是这样生成的!
sql日志
TCP三次握手四次挥手
ODOO开发教程之透视表
Climbing the pit of traffic flow prediction (II): the simplest LSTM predicts traffic flow using tensorflow2
Lenovo Savior r7000+ add ssd+ copy and partition the information of the original D disk to the new SSD
Go memory model for concurrency
Improve the readability of your regular expressions a hundred times
Use annotation test in idea
2021-11-02
如何安装office2010安装包?office2010安装包安装到电脑上的方法
Excel怎么筛选出自己想要的内容?excel表格筛选内容教程
P1009 [noip1998 popularization group] sum of factorials
SGuard64.exe ACE-Guard Client EXE:造成磁盘经常读写,游戏卡顿,及解决方案
tmux随笔
What servers are needed to build mobile app