1
所以我不是最好的SQL我有這個複雜的MySQL查詢,我迷失了。部分我不明白的是,所有的結果出來的部分查詢上面的UNION乘以8.這怎麼可能?他們爲什麼乘以8,我該如何阻止它?我還有其他一些與此類似的查詢,他們都有這個問題。任何想法爲什麼這可能會發生將不勝感激。謝謝!MySQL非常困惑查詢結果
SELECT best_thrown
FROM(
SELECT CASE WHEN rageup_echelon.party_id IS NULL THEN 0 ELSE SUM(CASE WHEN rageup_echelon.echelon_vote = 1 THEN 5 ELSE 0 END) - SUM(CASE WHEN rageup_echelon.echelon_vote = 0 THEN 3 ELSE 0 END) END AS best_thrown
FROM rageup_parties LEFT JOIN rageup_echelon ON rageup_parties.party_id = rageup_echelon.party_id
LEFT JOIN rageup_team_hosts ON rageup_parties.party_team_id = rageup_team_hosts.team_id
WHERE rageup_team_hosts.team_id = 1 || rageup_team_hosts.team_id = 2 || rageup_team_hosts.team_id = 3
AND rageup_parties.party_status = 'approved'
AND rageup_parties.party_invites = 'public'
AND rageup_team_hosts.user_id = 1
GROUP BY rageup_parties.party_id
UNION
SELECT CASE WHEN rageup_echelon.party_id IS NULL THEN 0 ELSE SUM(CASE WHEN rageup_echelon.echelon_vote = 1 THEN 5 ELSE 0 END) - SUM(CASE WHEN rageup_echelon.echelon_vote = 0 THEN 3 ELSE 0 END) END AS best_thrown
FROM rageup_parties LEFT JOIN rageup_echelon
ON rageup_parties.party_id = rageup_echelon.party_id
WHERE rageup_parties.party_host_id = 1
AND rageup_parties.party_status = 'approved'
AND rageup_parties.party_invites = 'public'
AND rageup_parties.party_team_id = 'user'
GROUP BY rageup_parties.party_id
ORDER BY best_thrown DESC
LIMIT 1
) AS T
謝謝你回覆如此之快的阿蘭。我試着選擇*並帶走GROUP BY。這幫助了我一點點。它每次返回每個結果8次,唯一不同的是,team_hosts表的索引從1到8計數。而team_hosts表中的這8行對於兩個team_id列都具有相同的值「1」和user_id列。這絕對是解釋一些東西,但我仍然不知道如何解決它。我將這些條件移到ON子句,謝謝,我不認爲這是確切的問題。 –
所以基本上我怎樣才能得到這個查詢來檢查team_id和user_id的team_hosts表,但不能使用每一行都有正確的team_id和user_id;只有一行? –
哈哈問題實際上是與ON條款。謝謝! –