当前位置:网站首页>MySql关键字GROUP_CONCAT,组合连接查询
MySql关键字GROUP_CONCAT,组合连接查询
2022-04-23 03:15:00 【小道仙97】
一、
试着想一下如果有这样一个需求应该怎么去做:
有一张学生表,和一张分数表。要查出每个学生姓名和分数,分数以逗号隔开
eg:张三 80,90,100
这个需求很简单,有很多种办法来解决,最简单的是我们可以先查询学生,然后再去查询分数,最后由程序代码组装。
学生表(stu)
id | name |
---|---|
1 | 张三 |
2 | 李四 |
3 | 王五 |
分数表(course)
id | name | stu_id | score |
---|---|---|---|
1 | 语文 | 1 | 80 |
2 | 数学 | 1 | 90 |
3 | 英语 | 1 | 75 |
4 | 语文 | 2 | 55 |
5 | 数学 | 2 | 45 |
6 | 英语 | 3 | 90 |
二、
但是前段时间朋友推荐了一个MySql关键字group_concat,使用这个关键字就很简单了。
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
三、
如果我们要查询科目名、分数,并且以分号分割应该怎么写呢?
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
在group_concat里面的字段,不能有为null,不然就会整体为null
版权声明
本文为[小道仙97]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Tomwildboar/article/details/115207797
边栏推荐
- Huawei mobile ADB devices connection device is empty
- Yes Redis using distributed cache in NE6 webapi
- 可以接收多种数据类型参数——可变参数
- Chapter 9 of C language programming (fifth edition of Tan Haoqiang) analysis and answer of exercises for users to establish their own data types
- The whole network is the most complete. How to do interface automation test? Proficient in interface automation test details
- xutils3修改了我提报的一个bug,开心
- MySQL索引详解【B+Tree索引、哈希索引、全文索引、覆盖索引】
- Ide-idea-problem
- 求二叉树的叶子结点个数
- 7-11 重排链表 (25 分)
猜你喜欢
Eight elder brothers chronicle [4]
OLED multi-level menu record
Experiment 6 input / output stream
全网讲的最细,软件测试度量,怎样优化软件测试成本提高效率---火爆
Blazor University (11) component - replace attributes of subcomponents
. net tip: talk about the problem that the scoped service cannot be obtained in the middleware structure
C WPF UI framework mahapps switching theme
关于idea调试模式下启动特别慢的优化
Blazor University (12) - component lifecycle
2022 P cylinder filling training test questions and simulation test
随机推荐
Flink real-time data warehouse project - Design and implementation of DWS layer
ASP. Net and ASP NETCORE multi environment configuration comparison
Tencent video VIP member, weekly card special price of 9 yuan! Tencent official direct charging, members take effect immediately!
2022t elevator repair test simulation 100 questions and online simulation test
MySQL索引详解【B+Tree索引、哈希索引、全文索引、覆盖索引】
Charles uses three ways to modify requests and responses
2022T电梯修理考试模拟100题及在线模拟考试
【VS Code】解决jupyter文件在vs code中显示异常的问题
How does Microsoft solve the problem of multiple PC programs
MySQL port is occupied when building xampp
Preview of converting doc and PDF to SWF file
C语言实现通讯录----(静态版本)
C read / write binary file
Blazor University (11) component - replace attributes of subcomponents
Queue storage and circular queue
[MySQL] left Function | Right Function
队列的存储和循环队列
为什么BI对企业这么重要?
How to achieve centralized management, flexible and efficient CI / CD online seminar highlights sharing
Data mining series (3)_ Data mining plug-in for Excel_ Estimation analysis