当前位置:网站首页>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
边栏推荐
- C read / write binary file
- 超好用的Excel异步导出功能
- Middle and rear binary tree
- js 中,为一个里面带有input 的label 绑定事件后在父元素绑定单机事件,事件执行两次,求解
- yes. Net future
- [Mysql] LEFT函數 | RIGHT函數
- poi根据数据创建导出excel
- ASP. Net and ASP NETCORE multi environment configuration comparison
- How does Microsoft solve the problem of multiple PC programs
- EasyUI's combobox implements three-level query
猜你喜欢

This new feature of C 11, I would like to call it the strongest!

Fight leetcode again (290. Word law)

搭建XAMPP时mysql端口被占用

Tencent video VIP member, weekly card special price of 9 yuan! Tencent official direct charging, members take effect immediately!

Experiment 6 input / output stream

Is it difficult to choose binary version control tools? After reading this article, you will find the answer

Impact of AOT and single file release on program performance

Yes Redis using distributed cache in NE6 webapi

Due to 3 ²+ four ²= five ², Therefore, we call '3,4,5' as the number of Pythagorean shares, and find the array of all Pythagorean shares within n (including n).

OLED多级菜单记录
随机推荐
The most easy to understand service container and scope of dependency injection
ThreadLocal 测试多线程变量实例
超好用的Excel异步导出功能
全网讲的最细,软件测试度量,怎样优化软件测试成本提高效率---火爆
12. < tag linked list and common test site synthesis > - lt.234 palindrome linked list
准备一个月去参加ACM,是一种什么体验?
Due to 3 ²+ four ²= five ², Therefore, we call '3,4,5' as the number of Pythagorean shares, and find the array of all Pythagorean shares within n (including n).
Xamarin effect Chapter 21 expandable floating operation button in GIS
Preview of converting doc and PDF to SWF file
Mysql database, inconsistent index character set, slow SQL query, interface timeout
Source generator actual combat
JSON related
LoadRunner - performance testing tool
ASP. Net 6 middleware series - Custom middleware classes
JS recursive tree structure calculates the number of leaf nodes of each node and outputs it
The most easy to understand dependency injection and control inversion
Yes Redis using distributed cache in NE6 webapi
2022a special equipment related management (elevator) work license question bank and simulation examination
The backtracking of stack is used to solve the problem of "the longest absolute path of file"
MySQL port is occupied when building xampp