当前位置:网站首页>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
```=

-
边栏推荐
- 2021-10-11
- How is the entered query SQL statement executed?
- [2022 freshmen learning] key points of the third week
- 1 sentence of code, get asp Net core binds multiple sources to the same class
- Original code, inverse code, complement code
- The method and detailed code of automatically pop-up and QQ group when players visit the website
- How does WPS use smart fill to quickly fill data? WPS method of quickly filling data
- 【文件下载】Easyexcel快速上手
- AttributeError: ‘module‘ object has no attribute ‘create_ connection‘
- The person who goes to and from work on time and never wants to work overtime has been promoted in front of me
猜你喜欢

The method and detailed code of automatically pop-up and QQ group when players visit the website

Mysql把查询到的结果集按指定顺寻进行排序

MySQL regularly calls preset functions to complete data update

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

向往的开源之多YOUNG新生 | 从开源到就业的避坑指南来啦!

How to monitor micro web services

传奇开区网站如何添加流量统计代码

【2022新生学习】第三周要点
![[file download] easyexcel quick start](/img/fd/b3b08c67c5a81f0ab58c20653f9398.png)
[file download] easyexcel quick start

Use openmap and ArcGIS to draw maps and transportation networks of any region, and convert OMS data into SHP format
随机推荐
Climbing the pit of traffic flow prediction (II): the simplest LSTM predicts traffic flow using tensorflow2
Northeast University Data Science Foundation (matlab) - Notes
Create a mindscore environment in modelars, install mindvision, and conduct in-depth learning and training (Huawei)
[file download] easyexcel quick start
Open the tutorial of adding and modifying automatically playing music on the open zone website
P2181 diagonal
Jackson parsing JSON detailed tutorial
传奇开区网站如何添加流量统计代码
Spark的算子操作列表
学习数据库的第一个程序
Sparksql inserts or updates in batches and saves data to MySQL
Apache POI实现Excel导入读取数据和写入数据并导出
关于servlet中实现网站的页面跳转
How to set row height and column width in excel? The method of setting row height and column width in Excel
Cache penetration, cache breakdown, cache avalanche and Solutions
如何安装office2010安装包?office2010安装包安装到电脑上的方法
力扣------对奇偶下标分别排序
Office提示系统配置无法运行怎么办?
How to solve the problem of configuring the progress every time Office2010 is opened?
Five correlation analysis, one of the most important skills of data analysts