MySQL从另一个表中选择前4名男性分数和前2名女性分数

时间:2014-03-08 22:07:20

标签: mysql join

我有一个MySQL查询问题,我觉得它需要加入表格,但我在加入查询方面不是很有经验,并且想知道是否有人可以帮助我?

我有两张桌子。第一个被称为“大学”,看起来像这样:

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

我要做的是找到每个大学的前4名男性分数和每轮(r1 - r2)的前2名女性分数的总和,并返回每轮的总数。然后还将它们加在一起,为每所大学的所有轮次制作总分,然后按DESC顺序排列大学行。

所以返回表可能看起来像这样

university.name | r1total | r2total | r3total | r4total | totalscore
---------------------------------------------------------------------
uni2name        | 600     | 800     | 900     | 1100    | 3400

uni1name        | 450     | 600     | 900     | 1100    | 3050

非常感谢任何有关此问题的帮助。

1 个答案:

答案 0 :(得分:2)

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

Fiddle

相关问题