mysql - 一条SQL语句代替两条

标签 mysql

我正在尝试缩短我的代码,但我被卡住了。目前我执行两个单独的查询以从两个单独的表中获取我需要的数据。前提是生成一份报告,根据性别和种族比较两个不同类(class)的考试成绩。

查询 1:

SELECT g.ethnicity AS dataPointA, 
count(case when t.fall_spring = 1 then t.studentID end) as countA,
round(avg(case when t.fall_spring = 1 then t.compScore end),0) as scoreA,
round(avg(case when t.fall_spring = 1 then t.compNP end),1) as scoreNPA

from table1 t
JOIN aall_students s
ON t.studentID=s.studentID 
JOIN aall_ethnicities g
ON s.ethnicityID=g.ethnicityID
WHERE t.classof = 2021 and s.gender = 'F'
group by s.ethnicityID

Results query 1:

dataPointA        countA   scoreA    scoreNPA
--------------------------------------------
African American |   18   | 914   |   56.6  |
--------------------------------------------
Asian            |    8   | 998   |   71.4  |
--------------------------------------------
Hispanic/Latino  |    5   | 936   |   63.0  |
--------------------------------------------
Two or More      |   11   | 1005  |   72.3  |
--------------------------------------------
White            |   28   | 1028  |   76.7  |

查询 2: 请注意,第二个查询中的 WHERE 子句不同,返回的行数也不同。

SELECT g.ethnicity AS dataPointB, 
count(case when t.fall_spring = 1 then t.studentID end) as countB,
round(avg(case when t.fall_spring = 1 then t.compScore end),0) as scoreB,
round(avg(case when t.fall_spring = 1 then t.compNP end),1) as scoreNPB

from table2 t
JOIN aall_students s
ON t.studentID=s.studentID 
JOIN aall_ethnicities g
ON s.ethnicityID=g.ethnicityID
WHERE t.classof = 2022 and s.gender = 'F'
group by s.ethnicityID

Results query 2:

dataPointB        countB   scoreB    scoreNPB
----------------------------------------------
African American |   12   | 838    |   40.0   |
----------------------------------------------
Asian            |    7   | 957    |   65.9   |
----------------------------------------------
Hispanic/Latino  |    7   | 931    |   59.6   |
----------------------------------------------
Native American  |    1   | 940    |   64.0   |
----------------------------------------------
Two or More      |   18   | 963    |   66.1   |
----------------------------------------------
White            |   32   | 1074   |   84.1   |

请注意,查询返回的行数不同,这意味着第一个查询中没有美洲原住民,第二个查询中没有。

如果我可以将两个查询合并为一个查询,那么查询和处理就会更少,例如,来自两个查询的非裔美国人的所有数据都落在一行中。它还将消除脚本端的处理和潜在的错误。

我尝试了各种形式的 UNION、JOIN 和广泛的谷歌搜索,但无法提出任何解决方案。任何能为我指明正确方向的建议都将不胜感激。

想要的结果应该是这样的:

dataPointA        countA   scoreA    scoreNPA   dataPointB       countB   scoreB   scoreNPB
--------------------------------------------------------------------------------------------
African American |   18   | 914   |   56.6  |  African American |   12   | 838   |   40.0  |
--------------------------------------------------------------------------------------------
Asian            |    8   | 998   |   71.4  |  Asian            |    7   | 957   |   65.9  |
--------------------------------------------------------------------------------------------
Hispanic/Latino  |    5   | 936   |   63.0  |  Hispanic/Latino  |    7   | 931   |   59.6  |
--------------------------------------------------------------------------------------------
null             |  null  | null  |   null  |  Native American  |    1   | 940   |   64.0  |
--------------------------------------------------------------------------------------------
Two or More      |   11   | 1005  |   72.3  |  Two or More      |   18   | 963   |   66.1  |
--------------------------------------------------------------------------------------------
White            |   28   | 1028  |   76.7  |  White            |   32   | 1074  |   84.1  |

使用 UNION ALL 我得到了一个可行的数组,这需要在代码端进行一些操作。我还添加了回复中建议的“类”列以区分表格:

dataPoint           class     count    score      scoreNP   
------------------------------------------------------------
African American |  2021   |   18    |  914    |   56.6    |
------------------------------------------------------------
Asian            |  2021   |    8    |  998    |   71.4    |
------------------------------------------------------------
Hispanic/Latino  |  2021   |    5    |  936    |   63.0    |
------------------------------------------------------------
Two or More      |  2021   |   11    | 1005    |   72.3    |
------------------------------------------------------------
White            |  2021   |   28    | 1028    |   76.7    |
------------------------------------------------------------
African American |  2022   |   12    |  838    |   40.0    |
------------------------------------------------------------
Asian            |  2022   |    7    |  957    |   65.9    |
------------------------------------------------------------
Hispanic/Latino  |  2022   |    7    |  931    |   59.6    |
------------------------------------------------------------
Native American  |  2022   |    1    |  940    |   64.0    |
------------------------------------------------------------
Two or More      |  2022   |   18    |  963    |   66.1    |
------------------------------------------------------------
White            |  2022   |   32    | 1074    |   84.1    |

最佳答案

This is a job for UNION ALL .为什么全部?普通的 UNION 会尝试对行进行重复数据删除,而您不需要它。

尝试这样做:

     SELECT 'A' datatype, whatever, whatever
       FROM table1 ... WHERE whatever
     UNION ALL
     SELECT 'B' datatype, whatever, whatever
       FROM table2 ... WHERE whatever

您将获得一个结果集,其中包含一个名为 datatype 的额外列(在我的示例中)。您可以使用该列来区分两个不同表格的结果。

关于mysql - 一条SQL语句代替两条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54671677/

相关文章:

php - 发送 MySQL 查询,无需表单或页面刷新

java - 生成格式错误的字符串以进行测试

MySQL LIMIT 0,15 其中 15 是 parent_ids 的数量,不是 child

PHP:在数据库中存储动态生成的文本字段值

sql - 仅当一个字段匹配多个值时才匹配(错误?)

mysql - 在 MySQL 中选择对同一临时表的不同列的计数

mysql - #1054 - 'on clause' 中的未知列

mysql - Centos 6 64位启用my.cnf

mysql - 简单 MySQL 事件 #1064 - 您的 SQL 语法有错误;

php - 将购物车数据插入数据库