2013-08-26 46 views
2

我在訪問如下表2010年選擇列

EQID Breaker Circuit Rating 
1 A  One  1000 
2 A  Two  1500 
3 A  Three 500 
4 A  Four 1000 
5 B  One  1500 
6 B  Two  2000 

我想通過創建一個斷路器查詢組,並顯示最低評級和相關電路爲評分。我知道如何做到這一點,而不顯示電路的最低等級。

我想要的查詢結果是:

EQID Breaker Circuit Rating 
1 A  Three 500 
2 B  One  1500 
+2

顯示您嘗試過的查詢,我們將一起解決 – Eggplant

回答

3

試試這個:

SELECT a.* 
FROM table AS a 
INNER JOIN (
    SELECT Breaker, MIN(Rating) AS min_rating 
    FROM table 
    GROUP BY Breaker 
) AS b 
ON a.Breaker = b.Breaker AND 
    a.Rating = b.min_rating; 

SQLFiddle:http://www.sqlfiddle.com/#!2/ea4fb/2

+0

注意到了,謝謝。我只是在搖擺MySQL語法。 – Tricky12

+0

+1乍得很快。 –

+0

完美。謝謝乍得。我沒有意識到它的回答如此之快,沒有得到自動的回覆通知。 – pgans

1

您可以在下面嘗試:

SELECT t.EQID, t.Breaker, t.Circuit, t.Rating 
FROM test t 
INNER JOIN 
(
    SELECT a.Breaker, MIN(a.Rating) AS Rating 
    FROM test a 
    GROUP BY Breaker 
) AS tmp 
ON tmp.Breaker = t.Breaker AND tmp.Rating = t.Rating; 

的Sql小提琴DEM o:http://sqlfiddle.com/#!2/fe796/19

+0

訪問可能在這裏抱怨「循環引用」:'MIN(a.Rating)AS Rating'如果發生這種情況,您可以通過不重複使用字段名稱作爲別名來消除錯誤:MIN(a.Rating) AS MinOfRating' – HansUp

+0

很棒的HansUp。謝謝 –

+0

感謝安達和@Hansup的信息。它非常有幫助。 – pgans