当前位置:网站首页>MySQL PgSQL realizes the merging of multiple lines of records into one line, grouping and merging, and dividing with specified characters
MySQL PgSQL realizes the merging of multiple lines of records into one line, grouping and merging, and dividing with specified characters
2022-07-28 17:25:00 【wangxudongx】
mysql pgsql Group according to the specified fields to merge multiple rows into one row
Database server environment
| database | edition |
|---|---|
| mysql | 5.7 |
| PostgreSQL | 10.10 |
Original table structure and data
| id | name | gender | major |
|---|---|---|---|
| 1 | Zhang San | male | Basketball |
| 2 | Zhang San | male | football |
| 3 | Li Si | Woman | swimming |
| 4 | Xiaozhou | Woman | running |
| 5 | The king 2 | male | Bodybuilding |
| 6 | Xiaozhou | Woman | Tennis |
| 7 | Zhang San | male | football |
mysql
SQL
select demo_table.name, group_concat(major separator ',') as combine_name
from demo_table
group by name
result
| name | combine_name |
|---|---|
| Xiaozhou | running , Tennis |
| Zhang San | Basketball , football , football |
| Li Si | swimming |
| The king 2 | Bodybuilding |
duplicate removal
select demo_table.name, group_concat(distinct major separator ',') as combine_name
from demo_table
group by name
Query results after de duplication
| name | combine_name |
|---|---|
| Xiaozhou | Tennis , running |
| Zhang San | Basketball , football |
| Li Si | swimming |
| The king 2 | Bodybuilding |
pgsql
SQL
select name ,
array_to_string(array (select unnest(array_agg(major))), ',') as combination_name
from demo_table
group by name
result
| name | combination_name |
|---|---|
| Zhang San | Basketball , football , football |
| Li Si | swimming |
| Xiaozhou | running , Tennis |
| The king 2 | Bodybuilding |
duplicate removal
select name ,
array_to_string(array (select unnest(array_agg(distinct major))), ',') as combination_name
from demo_table
group by name
Results after weight removal
| name | combination_name |
|---|---|
| Xiaozhou | Tennis , running |
| Zhang San | Basketball , football |
| Li Si | swimming |
| The king 2 | Bodybuilding |
Introduction to related articles
边栏推荐
- Unity shader realizes water wave effect with noise texture
- Unity shader procedural texture
- Codeforces Round #750 (Div. 2) F.Korney Korneevich and XOR (easy&&hard version)(dp)
- Goweb开发之Iris框架实战:项目总结与回顾
- In some cases, error: (XX, XX) failed to resolve: XXXXXX.
- Verilog daily question (vl24 multi bit MUX synchronizer cross time domain output)
- Verilog daily question (vl6 data series to parallel circuit)
- QR code generation of wechat applet with parameters
- 解决SQL Server数据库独占的问题
- Wechat applet cash red packet returns the error "the IP address is not the available IP address you set on the merchant platform". The ultimate solution
猜你喜欢

Verilog 每日一题(VL6 数据串转并电路)

The actual combat of the beego framework of goweb development: Section III program execution process analysis

火了 2 年的服务网格究竟给微服务带来了什么?(转载)

Jupyter notebook win installation record

Valarray Library Learning

Net framework

Round 1A 2022 - Code jam 2022 c.weightlifting (interval DP)

How do we do full link grayscale on the database?

Several methods of importing excel file data by C #

Verilog daily question (vl5 signal generator)
随机推荐
Linear algebra and matrix theory (VIII)
Mysql database addition, deletion, modification and query (detailed explanation of basic operation commands)
Linear algebra and matrix theory (7)
【kibana】问题整理 kibana 7.x No indices match pattern “apm-*“
Goweb开发之Beego框架实战:第四节 数据库配置及连接
配置V530交换机步骤
Goweb开发之Beego框架实战:第三节 程序执行流程分析
net框架
Make full use of English
Azure Devops developed by visual studio 2015 team
零基础利用Unity3D开发AR应用并远程下载3D模型
Linear algebra and matrix theory (10)
Selection and application of capacitor in high speed circuit -- detailed explanation
一文了解 Kubernetes 中的服务发现
DGL Chapter 1 (official tutorial) personal notes
Proof of the third scene (f) in 22 years
Verilog daily question (vl27 settable counter)
Use Alibaba cloud's free SSL certificate
How do we do full link grayscale on the database?
Codeforces round 770 (Div. 2) e. fair share