【发布时间】:2017-12-31 17:46:24
【问题描述】:
我有一个问题如下:
黑客的总分是他们在所有挑战中的最高分之和。编写查询以打印按分数降序排列的黑客的
hacker_id、姓名和总分。
如果不止一名黑客获得相同的总分,则将结果按升序排列hacker_id。
从您的结果中排除所有总分为 0 的黑客。
两张表如下:
表:黑客
========================================
hacker_id: Integer (ID of the hacker)
name: String (Name of the hacker)
========================================
表格:提交内容
===================================================
submission_id: Integer (ID of the submission)
hacker_id: Integer (ID of the hacker)
challenge_id: Integer (ID of the challenge)
score: Integer (score of the submission)
===================================================
我写的MYSQL查询如下:-
select
a.hacker_id,
a.name,
a.total
from(
select
h.hacker_id,
h.name,
sum(case when s.hacker_id=h.hacker_id then s.score else 0 end) as total
from
hackers h,
submissions s
group by
h.hacker_id,
h.name
) as a
where
a.total>0
order by
a.total desc,
a.hacker_id asc;
我得到了错误的输出,尽管输出满足了所有的顺序规则和 0 得分员的遗漏。我对错误是什么感到非常困惑。有人请帮忙!!!
【问题讨论】:
-
首先,使用正确的连接。您不需要两张表的笛卡尔积。
标签: mysql