当前位置:网站首页>How does MySQL convert rows to columns?
How does MySQL convert rows to columns?
2022-07-29 07:23:00 【Do you eat oil cake】
I don't say much nonsense , Let's look directly at an example :
This is a student transcript we have built student_score, Let's take a look at the results after the conversion , Here's the picture :
It can be seen that , Here, the row to column is to change the original subject Select multiple lines of the field , As different columns in the result set , According to userid Group and display the corresponding score, We generally have two ways to realize
1、 Use CASE...WHEN...THEN sentence
SELECT userid,
SUM(CASE `subject` WHEN ' Chinese language and literature ' THEN score ELSE 0 END) as ' Chinese language and literature ',
SUM(CASE `subject` WHEN ' mathematics ' THEN score ELSE 0 END) as ' mathematics ',
SUM(CASE `subject` WHEN ' English ' THEN score ELSE 0 END) as ' English ',
SUM(CASE `subject` WHEN ' Politics ' THEN score ELSE 0 END) as ' Politics '
FROM student_score
GROUP BY userid
for example CASE `subject` WHEN ' Chinese language and literature ’ THEN score ELSE 0 END This statement , That is, for all fields subject=` Chinese language and literature ` Conduct SUM() operation , Set the value to score, If not, set to 0
2、 Use IF() function
SELECT userid,
SUM(IF(`subject`=' Chinese language and literature ',score,0)) as ' Chinese language and literature ',
SUM(IF(`subject`=' mathematics ',score,0)) as ' mathematics ',
SUM(IF(`subject`=' English ',score,0)) as ' English ',
SUM(IF(`subject`=' Politics ',score,0)) as ' Politics '
FROM student_score
GROUP BY userid
IF(subject=` Chinese language and literature `,score,0) As a condition , For all subject=` Chinese language and literature ` The record of score The fields go SUM() operation , If score If there is no value, the default value is 0.
Be careful :SUM() To be able to use GROUP BY according to userid Grouping , Because every one of them userid Corresponding subject=" Chinese language and literature " There's only one record of , therefore SUM() The value of is equal to that of the corresponding record score Value . If userid ='001' and subject=' Chinese language and literature ' There are two records of , Now SUM() The value of will be the sum of these two records , Empathy , Use Max() The value of will be the largest of the two records . But normally , One user Corresponding to one subject There's only one score , So you can use SUM()、MAX()、MIN()、AVG() Aggregate function can achieve the effect of row to column .
边栏推荐
- Leetcode 879. profit plan
- Operator3-设计一个operator
- 2-统一返回类DTO对象
- 论文阅读 (62):Pointer Networks
- MySQL advanced (Advanced) SQL statement (I)
- Ethernet interface introduction
- 使用VsCode配置MySQL实现连接、查询、等功能
- My personal website doesn't allow access to wechat, so I did this
- [solution] error: lib/bridge_ generated. dart:837:9: Error: The parameter ‘ptr‘ of the method ‘FlutterRustB
- npm install 时,卡住不动,五种解决方法
猜你喜欢
亚马逊云助手小程序来啦!
1-后台项目搭建
Comparison of advantages between can & canfd integrated test analysis software lkmaster and PCA Explorer 6 analysis software
ERROR 1045 (28000) Access denied for user ‘root‘@‘localhost‘解决方法
微服务远程调用
Scala higher order (10): exception handling in Scala
Cvpr2021 | multi view stereo matching based on self supervised learning (cvpr2021)
Scala higher order (IX): pattern matching in Scala
WPF嵌套布局案例
3-global exception handling
随机推荐
反射reflect
JS 鸡生蛋与蛋生鸡问题,Object与Function究竟谁出现的更早?Function算不算Function的实例?
WPF 界面布局必知基础
2-统一返回类DTO对象
路由中的生命周期钩子 - activated与deactivated
Remote invocation of microservices
JS break and continue and return keywords
Vite3.0都发布了,你还能卷得动吗(新特性一览)
Can I specify memory parameters in SQL statements?
Operator3-设计一个operator
Comparison of advantages between can & canfd integrated test analysis software lkmaster and PCA Explorer 6 analysis software
[Charles' daily problems] when you open Charles, you can't use nails
npm install报错npm ERR Could not resolve dependency npm ERR peer
fillder使用
论文阅读 (62):Pointer Networks
20-40k | mecarmand 3D vision algorithm / software / Product Manager Recruitment
NPM install reports an error NPM err could not resolve dependency NPM err peer
After three years of outsourcing, the salary of automatic testing after job hopping is twice that of the original. The secret is
Student achievement ranking system based on C language design
gin 路由,参数,输出