当前位置:网站首页>MySQL many to many relationship, grouping and splicing to query multiple data to one data
MySQL many to many relationship, grouping and splicing to query multiple data to one data
2022-07-29 05:10:00 【nianyuw】
- GROUP_CONCAT(str): Grouping string splicing , And Use together in groups
Case study : Query enterprise information and enterprise classification information , Among them, the relationship between enterprise classification information and enterprise is many to many , Query according to the common joint table , We will find that one piece of enterprise information corresponds to multiple enterprise classifications , Multiple records will appear . If you want to classify different enterprises of the same enterprise into one record , You can consider using it GROUP_CONCAT, This function will splice multiple pieces of data onto one piece of data ( By default , Division )
Direct linked table query , You will see that the enterprise classification associated with an enterprise is multiple data
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

Use 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
```=

-
边栏推荐
猜你喜欢

How does excel filter out the content you want? Excel table filtering content tutorial

How to solve the problem of configuring the progress every time Office2010 is opened?

荣耀2023内推,内推码ambubk

TCP三次握手四次挥手

What if the computer cannot open excel? The solution of Excel not opening

如何安装office2010安装包?office2010安装包安装到电脑上的方法

Apache POI实现Excel导入读取数据和写入数据并导出

stack和queue和优先级队列(大堆和小堆)模拟实现和仿函数讲解

How to monitor micro web services

Quick start JDBC
随机推荐
A little knowledge about management
向往的开源之多YOUNG新生 | 从开源到就业的避坑指南来啦!
Double type nullpointexception in Flink flow calculation
如何让照片中的人物笑起来?HMS Core视频编辑服务一键微笑功能,让人物笑容更自然
The person who goes to and from work on time and never wants to work overtime has been promoted in front of me
Use jupyter (2) to establish shortcuts to open jupyter and common shortcut keys of jupyter
SM整合原来这么简单,步骤清晰(详细)
Spark的算子操作列表
Wechat picture identification
TCP三次握手四次挥手
缓存穿透、缓存击穿、缓存雪崩以及解决方法
Use openmap and ArcGIS to draw maps and transportation networks of any region, and convert OMS data into SHP format
Un7.28: common commands of redis client.
Mapper agent development
How to install Office2010 installation package? How to install Office2010 installation package on computer
【config】配置数组参数
Stack and queue and priority queue (large heap and small heap) simulation implementation and explanation of imitation function
What if the computer cannot open excel? The solution of Excel not opening
[wechat applet] swiper slides the page, and the left and right sides of the slider show part of the front and back, showing part of the front and back
ODOO开发教程之图表