我被困在如何修改我的查询以获得我正在寻找的排名结果。我一直在关注SO的问题和疑问,但无法得到结果。我可以得到查询来进行计算并返回数据,但“等级”不正确。任何轻推都会太棒了!
表1包含学校数据:
+-----+------------+---------------+
| SID | schoolName | schoolCountry |
+-----+------------+---------------+
| 1 | ASD | UAE |
| 2 | ASIJ | Japan |
| 3 | ASP | France |
+-----+------------+---------------+
表2包含审核数据(我的查询有更多列,但这是它的工作原理)。
+-----+----------+--------+----+----+----+----+----+
| RID | schoolID | active | Q1 | Q2 | Q3 | Q4 | Q5 |
+-----+----------+--------+----+----+----+----+----+
| 1 | 1 | 1 | 8 | 9 | 5 | 1 | 9 |
| 2 | 2 | 1 | 7 | 6 | 6 | 7 | 9 |
| 3 | 1 | 0 | 1 | 4 | 7 | 8 | 5 |
| 4 | 3 | 1 | 2 | 10 | 6 | 7 | 5 |
+-----+----------+--------+----+----+----+----+----+
我试图通过平均学校的整体评价得分来创建不同的等级(国家,地区,整体)。我目前正在进行国家排名,目前我的查询是。
SELECT SID, schoolName, rank, average
FROM (
SELECT (@rank := @rank + 1) AS rank,schools.SID, schools.schoolName,
ROUND(AVG(IF(reviews.active = 1, ((Q1+Q2+Q3+Q4+Q5+Q6+Q7+Q8+Q9+Q10+Q11+Q12+Q13+Q14+Q15+Q16+Q17+Q18+Q19+Q20+Q21+Q22+Q23+Q24+Q25+Q26+Q27+Q28+Q29+Q30+Q31+Q32+Q33+Q34+Q35+Q36+Q37+Q38+Q39+Q40+Q41+Q42+Q43+Q44+Q45+Q46+Q47+Q48+Q49+Q50+Q51+Q52)/(52*10)*10), NULL)) ,1) AS average
FROM schools
RIGHT JOIN reviews ON reviews.schoolID = schools.SID
CROSS JOIN (SELECT @rank := 0) AS vars
WHERE schools.schoolCountry = 'United Arab Emirates'
GROUP BY schools.SID
) as order_ranked
ORDER BY `order_ranked`.`average` DESC
我的输出回来了:
+-----+----------------------------------------+------+---------+--+
| SID | schoolName | rank | average | |
+-----+----------------------------------------+------+---------+--+
| 568 | GEMS Wellington Primary School | 3 | 8.3 | |
| 1 | American School of Dubai | 1 | 8.1 | |
| 561 | Dubai American Academy | 4 | 7.9 | |
| 560 | Deira International School | 11 | 7.7 | |
| 569 | GEMS World Academy Dubai | 10 | 7.0 | |
| 570 | Greenfield Community School | 8 | 6.7 | |
| 565 | GEMS American Academy Abu Dhabi | 6 | 6.0 | |
| 584 | Universal American School | 5 | 5.9 | |
| 558 | American Academy In Al Mizhar | 7 | 5.5 | |
| 579 | The Cambridge High School Abu Dhabi | 9 | 4.8 | |
| 576 | Ras Al Khaimah English Speaking School | 2 | 4.3 | |
+-----+----------------------------------------+------+---------+--+
你可以看到它排名,但不正确。我只是想不通为什么。
我不确定你为什么要使用right join
。在MySQL中,在使用变量之前,通常必须在子查询中进行排序。
SELECT SID, schoolName, (@rank := @rank + 1) AS rank, average
FROM (SELECT s.SID, s.schoolName,
ROUND(AVG(Q1+Q2+Q3+Q4+Q5+Q6+Q7+Q8+Q9+Q10+Q11+Q12+Q13+Q14+Q15+Q16+Q17+Q18+Q19+Q20+Q21+Q22+Q23+Q24+Q25+Q26+Q27+Q28+Q29+Q30+Q31+Q32+Q33+Q34+Q35+Q36+Q37+Q38+Q39+Q40+Q41+Q42+Q43+Q44+Q45+Q46+Q47+Q48+Q49+Q50+Q51+Q52)/(52*10)*10)) AS average
FROM schools s JOIN
reviews r
ON r.schoolID = s.SID
WHERE s.schoolCountry = 'United Arab Emirates' AND r.isactive = 1
GROUP BY schools.SID
ORDER BY average DESC
) sr CROSS JOIN
(SELECT @rank := 0) AS vars
ORDER BY average DESC
排名应该取决于平均排序。但内部查询中没有排序。首先计算平均值并计算其后的排名。此外,我将where
条件移动到join
,因为你拥有它的方式相当于一个内连接。
SELECT SID, schoolName, @rank := @rank + 1 AS rank, average
FROM (
SELECT schools.SID, schools.schoolName,
ROUND(AVG(IF(reviews.active = 1, ((Q1+Q2+Q3+Q4+Q5+Q6+Q7+Q8+Q9+Q10+Q11+Q12+Q13+Q14+Q15+Q16+Q17+Q18+Q19+Q20+Q21+Q22+Q23+Q24+Q25+Q26+Q27+Q28+Q29+Q30+Q31+Q32+Q33+Q34+Q35+Q36+Q37+Q38+Q39+Q40+Q41+Q42+Q43+Q44+Q45+Q46+Q47+Q48+Q49+Q50+Q51+Q52)/(52*10)*10), NULL)) ,1) AS average
FROM schools
RIGHT JOIN reviews ON reviews.schoolID = schools.SID AND schools.schoolCountry = 'United Arab Emirates'
GROUP BY schools.SID,schools.schoolName
) as order_ranked
CROSS JOIN (SELECT @rank := 0) AS vars
ORDER BY `average` DESC