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

-
边栏推荐
- Google gtest事件机制
- tmux随笔
- How does word view document modification traces? How word views document modification traces
- Improve the readability of your regular expressions a hundred times
- MySQL regularly calls preset functions to complete data update
- JDBC statement + resultset introduction
- 输入的查询SQL语句,是如何执行的?
- sql日志
- 带你搞懂 Kubernetes 集群中几种常见的流量暴露方案
- 后置通知的流程分析与功能实现有哪些内容你还记得吗?
猜你喜欢
How to monitor micro web services
Diagram of odoo development tutorial
Sparksql inserts or updates in batches and saves data to MySQL
【微信小程序--解决display:flex最后一行对齐问题。(不连续排列会分到两边)】
Improve the readability of your regular expressions a hundred times
WPS如何进行快速截屏?WPS快速截屏的方法
ODOO开发教程之透视表
[wechat applet -- solve the alignment problem of the last line of display:flex. (discontinuous arrangement will be divided into two sides)]
1 sentence of code, get asp Net core binds multiple sources to the same class
力扣------对奇偶下标分别排序
随机推荐
On prepayment of house purchase
Vivo market API event reporting and docking
Raspberry pie 4B + Intel neural computing stick (stick2) +yolov5 feasibility study report
WPS insert hyperlink cannot be opened. What should I do if I prompt "unable to open the specified file"!
Use jupyter (2) to establish shortcuts to open jupyter and common shortcut keys of jupyter
The representation of time series analysis: is the era of learning coming?
The difference between the two ways of thread implementation - simple summary
Connection database time zone setting
ThreadPoolExecutor simple to use
roLabelImg转DATO格式数据
Activity workflow table structure learning
Deadlock to be resolved
【文件下载】Easyexcel快速上手
pytorch学习笔记
MySQL time calculation function
虚拟偶像的歌声原来是这样生成的!
Opencv learning 1 (environment configuration)
[file download] easyexcel quick start
What if the computer cannot open excel? The solution of Excel not opening
P2181 diagonal