MySQL从另一个表中选择前4名男性得分和前2名女性得分
我有一个MySQL查询问题,我感觉它需要连接表,但我对连接查询不是很有经验,我想知道是否有人能帮我 我有两张桌子。第一个被称为“大学”,看起来像这样:MySQL从另一个表中选择前4名男性得分和前2名女性得分,mysql,join,Mysql,Join,我有一个MySQL查询问题,我感觉它需要连接表,但我对连接查询不是很有经验,我想知道是否有人能帮我 我有两张桌子。第一个被称为“大学”,看起来像这样: id | name 1 | University One 2 | University Two id | universityid | male | r1 | r2 | r3 | r4 我还有第二张表,叫做competitors,如下所示: id | name 1 | University One 2 | University Two
id | name
1 | University One
2 | University Two
id | universityid | male | r1 | r2 | r3 | r4
我还有第二张表,叫做competitors,如下所示:
id | name
1 | University One
2 | University Two
id | universityid | male | r1 | r2 | r3 | r4
其中“universityid”是指第一个表的外键,“male”是决定竞争对手性别的布尔值,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
我想做的是找出每所大学每一轮(r1-r2)的前4名男性成绩和前2名女性成绩的总和,并返回每一轮的总数。然后将它们相加,得出每所大学所有轮的总分,然后根据这些总分按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
每轮排名前4名的男性得分?显示一些样本数据或创建一个提琴。我已尝试编辑OP以使其更清晰。非常感谢您的帮助!这真是太棒了,除了如果某所大学在一轮比赛中没有进入一个完整的团队,那么它会重新计算分数。例如,如果一所大学在一轮比赛中只有一名男性参赛,那么它会将他的分数计算4次,而只需计算一次,其他分数则为0。有什么想法吗?对不起,我在OP中犯了另一个错误,我现在已经改正了。它应该返回大学的名称,而不是大学的id。我在我的电脑上试过了,最上面一行返回了大学名称中的所有0。第二行显示结果。。。但是,当我的测试数据中有三个UNI时,它也只显示太多行??顺便说一句,谢谢你在这件事上的帮助!这就是我得到的。。。也许这与测试数据中有零有关…?@jamesmcillveen还将图像添加到表数据中