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

-
边栏推荐
- 如何安装office2010安装包?office2010安装包安装到电脑上的方法
- How does WPS take quick screenshots? WPS quick screenshot method
- MySQL定时调用预置函数完成数据更新
- MySQL regularly calls preset functions to complete data update
- Wps如何使用智能填充快速填充数据?Wps快速填充数据的方法
- Deadlock to be resolved
- Conv2d of torch
- Office提示系统配置无法运行怎么办?
- TCP三次握手四次挥手
- [wechat applet -- solve the alignment problem of the last line of display:flex. (discontinuous arrangement will be divided into two sides)]
猜你喜欢

The person who goes to and from work on time and never wants to work overtime has been promoted in front of me

ODOO开发教程之透视表
带你搞懂 Kubernetes 集群中几种常见的流量暴露方案

Five correlation analysis, one of the most important skills of data analysts

What if excel is stuck and not saved? The solution of Excel not saved but stuck

Use annotation test in idea

【微信小程序--解决display:flex最后一行对齐问题。(不连续排列会分到两边)】

Office提示系统配置无法运行怎么办?

ODOO开发教程之图表

How does excel filter out the content you want? Excel table filtering content tutorial
随机推荐
学习数据库的第一个程序
Mapper agent development
Use annotation test in idea
Raspberry pie 4B + Intel neural computing stick (stick2) +yolov5 feasibility study report
The person who goes to and from work on time and never wants to work overtime has been promoted in front of me
电脑无法打开excel表格怎么办?excel打不开的解决方法
Learn matlab to draw geographical map, line scatter bubble density map
Rolabelimg to data format data
Big silent event Google browser has no title
How to debug UDP port
Mysql把查询到的结果集按指定顺寻进行排序
TMUX essays
< El table column> place multiple pictures
【文件下载】Easyexcel快速上手
Office提示系统配置无法运行怎么办?
Wps如何使用智能填充快速填充数据?Wps快速填充数据的方法
Use openmap and ArcGIS to draw maps and transportation networks of any region, and convert OMS data into SHP format
The difference between the two ways of thread implementation - simple summary
JS daily question (10)
Introduction of JDBC preparestatement+ database connection pool