我有一个 MySQL 查询问题,我感觉它需要连接表,但我在连接查询方面不是很有经验,想知道是否有人可以帮助我?
我有两张 table 。第一个称为“大学”,如下所示:
id | name
1 | University One
2 | University Two
我还有第二个表,看起来称为竞争对手,如下所示:
id | universityid | male | r1 | r2 | r3 | r4
其中“universityid”是引用第一个表的外键,“male”是确定参赛者性别的 bool 值,r1 - r4 是比赛不同轮次的得分。
所以这个表可能看起来像:
id | universityid | male | r1 | r2 | r3 | r4
-------------------------------------------------
1 | 1 | 1 | 200 | 100 | 150 | 200
2 | 1 | 1 | 50 | 100 | 150 | 200
3 | 1 | 1 | 50 | 100 | 150 | 200
4 | 1 | 1 | 50 | 100 | 150 | 200
5 | 1 | 0 | 50 | 100 | 150 | 150
6 | 1 | 0 | 50 | 100 | 150 | 150
7 | 2 | 1 | 200 | 200 | 150 | 200
8 | 2 | 1 | 200 | 100 | 150 | 200
9 | 2 | 1 | 50 | 100 | 150 | 200
10| 2 | 1 | 50 | 200 | 150 | 200
11| 2 | 0 | 50 | 100 | 150 | 150
12| 2 | 0 | 50 | 100 | 150 | 150
我想做的是找到每所大学每轮前 4 名男性分数和前 2 名女性分数的总和 (r1 - r2),并返回每轮的总分。然后还将它们加在一起得出每所大学所有轮次的总分,然后按这些总分按 DESC 顺序对大学行进行排序。
因此返回表可能如下所示
university.name | r1total | r2total | r3total | r4total | totalscore
---------------------------------------------------------------------
uni2name | 600 | 800 | 900 | 1100 | 3400
uni1name | 450 | 600 | 900 | 1100 | 3050
任何有关此问题的帮助将不胜感激。
最佳答案
SELECT universityid,SUM(r1sum),SUM(r2sum),SUM(r3sum),SUM(r4sum),
SUM(r1sum+r2sum+r3sum+r4sum)as TotalScore
FROM
(SELECT
universityid,
CAST(SUBSTRING_INDEX(
GROUP_CONCAT(r1 ORDER BY r1 DESC),
',', 1)as unsigned)+
CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(
GROUP_CONCAT(r1 ORDER BY r1 DESC),',0'),
',', 2),',',-1)as unsigned)+
CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(
GROUP_CONCAT(r1 ORDER BY r1 DESC),',0'),
',', 3),',',-1)as unsigned)+
CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(
GROUP_CONCAT(r1 ORDER BY r1 DESC),',0'),
',', 4),',',-1)as unsigned)as r1sum,
CAST(SUBSTRING_INDEX(
GROUP_CONCAT(r2 ORDER BY r2 DESC),
',', 1)as unsigned)+
CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(
GROUP_CONCAT(r2 ORDER BY r2 DESC),',0'),
',', 2),',',-1)as unsigned)+
CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(
GROUP_CONCAT(r2 ORDER BY r2 DESC),',0'),
',', 3),',',-1)as unsigned)+
CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(
GROUP_CONCAT(r2 ORDER BY r2 DESC),',0'),
',', 4),',',-1)as unsigned)as r2sum,
CAST(SUBSTRING_INDEX(
GROUP_CONCAT(r3 ORDER BY r3 DESC),
',', 1)as unsigned)+
CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(
GROUP_CONCAT(r3 ORDER BY r3 DESC),',0'),
',', 2),',',-1)as unsigned)+
CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(
GROUP_CONCAT(r3 ORDER BY r3 DESC),',0'),
',', 3),',',-1)as unsigned)+
CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(
GROUP_CONCAT(r3 ORDER BY r3 DESC),',0'),
',', 4),',',-1)as unsigned)as r3sum,
CAST(SUBSTRING_INDEX(
GROUP_CONCAT(r4 ORDER BY r4 DESC),
',', 1)as unsigned)+
CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(
GROUP_CONCAT(r4 ORDER BY r4 DESC),',0'),
',', 2),',',-1)as unsigned)+
CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(
GROUP_CONCAT(r4 ORDER BY r4 DESC),',0'),
',', 3),',',-1)as unsigned)+
CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(
GROUP_CONCAT(r4 ORDER BY r4 DESC),',0'),
',', 4),',',-1)as unsigned)as r4sum
FROM
competitors
WHERE male=1
GROUP BY
universityid
UNION ALL
SELECT
universityid,
CAST(SUBSTRING_INDEX(
GROUP_CONCAT(r1 ORDER BY r1 DESC),
',', 1)as unsigned)+
CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(
GROUP_CONCAT(r1 ORDER BY r1 DESC),',0'),
',', 2),',',-1)as unsigned)as r1sum,
CAST(SUBSTRING_INDEX(
GROUP_CONCAT(r2 ORDER BY r2 DESC),
',', 1)as unsigned)+
CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(
GROUP_CONCAT(r2 ORDER BY r2 DESC),',0'),
',', 2),',',-1)as unsigned)as r2sum,
CAST(SUBSTRING_INDEX(
GROUP_CONCAT(r3 ORDER BY r3 DESC),
',', 1)as unsigned)+
CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(
GROUP_CONCAT(r3 ORDER BY r3 DESC),',0'),
',', 2),',',-1)as unsigned)as r3sum,
CAST(SUBSTRING_INDEX(
GROUP_CONCAT(r4 ORDER BY r4 DESC),
',', 1)as unsigned)+
CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(
GROUP_CONCAT(r4 ORDER BY r4 DESC),',0'),
',', 2),',',-1)as unsigned)as r4sum
FROM
competitors
WHERE male=0
GROUP BY
universityid)x
GROUP BY universityid
ORDER BY TotalScore DESC
关于MySQL从另一个表中选择前4名男性分数和前2名女性分数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22275677/