当前位置:网站首页>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 cartoon style rendering
- The practice of beego framework developed by goweb: Section 4 database configuration and connection
- influxdb2的使用
- Goweb开发之Beego框架实战:第四节 数据库配置及连接
- Analysis of kubernetes service principle
- Steps to configure V530 switch
- 2021年4月份自考
- Using SQL server agent job to restore the database regularly
- 【impala】【报错解决】 Impala cannot read or execute the parent directory of dfs.domain.socket.path的解决方法
- Differences between CNSA and CASC and CASIC
猜你喜欢

Selection of resistance in high speed circuit

Janus series article 3 API usage guide videoroom creating a new video room

Analysis of browser decoding process

Unity shader cartoon style rendering

Analysis of kubernetes service principle

Verilog daily question (vl29 single port RAM)

Proof of the third scene (f) in 22 years

valarray数值库学习

Verilog 每日一题(VL14 自动贩售机1--FSM常见题型)

异步电路设计--同步脉冲器原理及例题
随机推荐
MySQL detailed learning tutorial (recommended Collection)
线性代数及矩阵论(七)
Codeforces round 770 (Div. 2) F. Fibonacci additions (construction + difference)
Verilog daily question (vl27 settable counter)
Verilog 每日一题(VL2 异步复位的串联T触发器--牛客网)
Modeling Semantics with Gated Graph Neural Networks for KBQA
高速电路中电容的选型和应用——详解
Unity shader screen post-processing
Use Alibaba cloud's free SSL certificate
【presto 】presto 新版本升级详情
The practice of beego framework developed by goweb: Section 4 database configuration and connection
Linear algebra and matrix theory (VIII)
【impala】【报错解决】 Impala cannot read or execute the parent directory of dfs.domain.socket.path的解决方法
Educational codeforces round 126 (rated for Div. 2) f.teleporters (two sets and two points)
2022 Niuke multi school second CDE
Selection and application of capacitor in high speed circuit -- detailed explanation
线性代数及矩阵论(九)
22年多校第三场(F的证明
Gray code and binary conversion and typical examples (4bits gray code counter)
valarray数值库学习