当前位置:网站首页>MySQL keyword group_ Concat, combined connection query
MySQL keyword group_ Concat, combined connection query
2022-04-23 03:16:00 【Xiaodaoxian 97】
One 、
Try to think about what to do if there is such a need :
There is a student list , And a score sheet . To find out the name and score of each student , Fractions are separated by commas
eg: Zhang San 80,90,100
This demand is very simple , There are many ways to solve , The simplest thing is that we can query the students first , Then check the score , Finally, it is assembled by program code .
Student list (stu)
| id | name |
|---|---|
| 1 | Zhang San |
| 2 | Li Si |
| 3 | Wang Wu |
Score table (course)
| id | name | stu_id | score |
|---|---|---|---|
| 1 | Chinese language and literature | 1 | 80 |
| 2 | mathematics | 1 | 90 |
| 3 | English | 1 | 75 |
| 4 | Chinese language and literature | 2 | 55 |
| 5 | mathematics | 2 | 45 |
| 6 | English | 3 | 90 |
Two 、
But some time ago, a friend recommended MySql keyword group_concat, It's easy to use this keyword .
SELECT
s.name name,
GROUP_CONCAT(c.score) score
FROM
stu s
LEFT JOIN course c ON s.id = c.stu_id
GROUP BY s.id

3、 ... and 、
If we want to query the account name 、 fraction , And divided by semicolon, how should I write ?
SELECT
s.name name,
GROUP_CONCAT(c.name, ':',c.score separator '; ') score
FROM
stu s
LEFT JOIN course c ON s.id = c.stu_id
GROUP BY s.id

stay group_concat The fields inside , You can't do anything null, Otherwise, the whole will be null
版权声明
本文为[Xiaodaoxian 97]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230315096690.html
边栏推荐
- LoadRunner - performance testing tool
- 手机连接电脑后,QT的QDIR怎么读取手机文件路径
- C language to achieve address book - (static version)
- Recommend reading | share the trader's book list and ask famous experts for trading advice. The trading is wonderful
- 超好用的【通用Excel导入功能】
- Configuration table and page information automatically generate curd operation page
- The most easy to understand dependency injection and control inversion
- Swap the left and right of each node in a binary tree
- C introduction of variable parameter params
- 2022t elevator repair test simulation 100 questions and online simulation test
猜你喜欢

TCP three handshakes and four waves

svg标签中利用<polygon/>循环数组绘制多边形

Top 9 task management system in 2022

超好用的Excel异步导出功能

Student achievement management

編碼電機PID調試(速度環|比特置環|跟隨)

LoadRunner - performance testing tool

How to achieve centralized management, flexible and efficient CI / CD online seminar highlights sharing

Why is bi so important to enterprises?

Ide-idea-problem
随机推荐
JS recursive tree structure calculates the number of leaf nodes of each node and outputs it
js递归树结构计算每个节点的叶子节点的数量并且输出
一文了解全面静态代码分析
It can receive multiple data type parameters - variable parameters
Iotos IOT middle platform is connected to the access control system of isecure center
After the mobile phone is connected to the computer, how can QT's QDIR read the mobile phone file path
Fundamentals of software testing and development
2022A特种设备相关管理(电梯)上岗证题库及模拟考试
Top 9 task management system in 2022
Chapter 9 of C language programming (fifth edition of Tan Haoqiang) analysis and answer of exercises for users to establish their own data types
Data mining series (3)_ Data mining plug-in for Excel_ Estimation analysis
Preview of converting doc and PDF to SWF file
Utgard connection opcserver reported an error caused by: org jinterop. dcom. common. JIRuntimeException: Access is denied. [0x800
[vs Code] solve the problem that the jupyter file displays exceptions in vs code
JS implementation of new
2022g2 boiler stoker examination question bank and online simulation examination
[untitled]
Blazor University (11) component - replace attributes of subcomponents
Mise en service PID du moteur de codage (anneau de vitesse | anneau de position | suivant)
队列的存储和循环队列