当前位置:网站首页>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 to set row height and column width in excel? The method of setting row height and column width in Excel
- JDBC statement + resultset introduction
- MySQL time calculation function
- 网安学习-内网安全1
- "Invisible Bridge" built in the free trade economy: domestic products and Chinese AI power
- Excel卡住了没保存怎么办?Excel还没保存但是卡住了的解决方法
- P1009 [noip1998 popularization group] sum of factorials
- Flink+iceberg environment construction and production problem handling
- Legend how to configure multiple versions of wechat updates on one server
- pytorch学习笔记
猜你喜欢
随机推荐
2021-10-11
Quick start JDBC
Rolabelimg to data format data
AttributeError: ‘module‘ object has no attribute ‘create_connection‘
向往的开源之多YOUNG新生 | 从开源到就业的避坑指南来啦!
Deadlock analysis using jstack, jconsole, and jvisualvm
The most comprehensive promotion plan for the launch of new products
Network Security Learning - Intranet Security 1
Glory 2023 push, push code ambubk
JS (in ES6) sync & await understanding
Connection database time zone setting
Live in small private enterprises
AttributeError: ‘module‘ object has no attribute ‘create_ connection‘
Vivo market API event reporting and docking
Huawei ilearning AI mathematics foundation course notes
Learn matlab to draw geographical map, line scatter bubble density map
Opencv learning 1 (environment configuration)
Pytorch learning notes
Mysql把查询到的结果集按指定顺寻进行排序
【文件下载】Easyexcel快速上手