我有一個問題如下:輸出錯在MYSQL
黑客的總得分是他們的最高分數的所有挑戰的總和。撰寫查詢以打印
hacker_id
,名稱和按降序排列的黑客總分。
如果不止一個黑客獲得相同的總分,則按升序排序hacker_id
。
從結果中排除總分爲0的所有黑客。
2.表中給出如下:
表:黑客
========================================
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;
我得到的輸出錯誤這雖然那輸出滿足要求爲了& ommission的0得分的所有規則。對於錯誤是什麼我很困惑。有人請幫助!
首先,使用正確的連接。你不需要這兩張表的笛卡爾積。 – Eric