2017-10-14 53 views
0

ERROR 1054(42S22):一個match_results(:未知列在 'where子句'什麼是錯的這個sql查詢ERROR 1054(42S22)

(select m.match_id,m.team1,m.team2,m.team1_score from match_results as m where m.team1_score=( select max(score) from ( select p.team1_score as score from match_results p where p.match_id=m.match_id UNION select q.team2_score as score from match_results q where q.match_id=m.match_id ) as T )) UNION (select m1.match_id,m1.team1,m1.team2,m1.team2_score from match_results m1 where m1.team2_score=( select max(score) from ( select team1_score as score from match_results where match_id=m1.match_id UNION select team2_score as score from match_results where match_id=m1.match_id )as T ));

模式 'm.match_id' match_id,team1,team2,team1_score,team2_score) team1和team2是varchar() team1_score和team2_score是整數。 我想取隊以最高分(TEAM1_SCORE或TEAM2_SCORE))

+1

您需要發佈架構和錯誤消息或您需要的操作。 – user2182349

+0

你有什麼錯誤? –

+0

您在UNION的兩個查詢中都沒有選擇相同數量的列。第一個查詢在select中沒有'match_id'。你需要在第一個選擇查詢的開始處添加'm.match_id' –

回答

2

(TEAM1或TEAM2)我已經簡化了查詢

select m.team1,m.team2,m.team1_score 
    from match_results as m, 
     match_results as p 
    where m.team1_score= greatest (p.team1_score, p.team2_score) 
    AND m.match_id = p.match_id 

UNION 

select m.team1,m.team2,m.team2_score 
    from match_results as m, 
     match_results as p 
    where m.team2_score= greatest (p.team1_score, p.team2_score) 
    AND m.match_id = p.match_id 

會讓你有相同的match_id重複一次以上? (邏輯上沒有!)如果是這樣,使用上面的查詢,否則你甚至使用簡單的

如果team1的得分最高,此查詢將給出team1的得分。它會給TEAM2的得分,如果TEAM2得分比TEAM1

select m.team1, m.team2, greatest (m.team1_score, m.team2_score) 
     from match_results as m 
更高
+0

感謝噸....最大()解決了一切,但我仍然想知道在發佈問題時,即使在刪除別名T或給出不同的別名後,我在問題中發佈的查詢中可能出現了什麼問題,但我也遇到了同樣的錯誤,請您對此進行評論? –

+0

我甚至想知道這個錯誤。嘗試在where子句中僅使用一個查詢運行查詢..我的意思是m.team1_score =從match_results p中選擇max(p.team1_score)得分,其中p.match_id = m.match_id ..查看是否仍有錯誤 – Valli

+0

但是不會滿足我想要實現的,可能是相關查詢的原因是應立即在內部查詢中指定相關性 –

0

ERROR 1054(42S22)

這個錯誤意味着你的領域之一有錯誤的名稱。
要麼是別名,要麼使用錯誤的別名,要麼只是錯誤的字段名稱。
此外,爲什麼你的查詢中有兩個AS t