2010-04-19 222 views
1

我創建了一個表幫助下結構 -需要一個SQL查詢

$sql = "CREATE TABLE followers 
    (
    uid int UNSIGNED NOT NULL UNIQUE, 
    PRIMARY KEY(uid), 
    follower_count int UNSIGNED , 
    is_my_friend bool, 
    status_count int UNSIGNED, 
    location varchar(50) 
    )"; 

我需要找到的人,最大的UID(status_count + FOLLOWER_COUNT),其is_my_friend = 1

我寫了下面的查詢,但我沒有得到正確的用戶名。

SELECT p.uid FROM (select uid,is_my_friend,max(follower_count+status_count) from followers) p WHERE p.is_my_friend = 1; 
+0

如果有什麼是相同的最大一個以上的用戶? – 2010-04-19 11:41:42

+0

@Mark:好的...我的代碼可以處理 – Bruce 2010-04-19 11:43:40

回答

3

視圖下面的查詢將工作:在MySQL

Select uid 
From followers 
Where is_my_friend = 1 
Order By (follower_count+status_count) desc LIMIT 0,1 

限制0,1作品。

或者,如果您想返回所有行FOLLOWER_COUNT + status_count = MAX而已,這是查詢:

Select uid 
From followers 
Where is_my_friend = 1 
    And (follower_count+status_count) = (select max(follower_count+status_count) 
             from followers 
             where is_my_friend = 1) 
1
SECLECT uid FROM followers ORDER BY (follower_count + status_count) DESC WHERE is_my_friend = 1 
+0

@Svisstack:SELECT uid FROM followers WHERE is_my_friend = 1 ORDER BY(follower_count + status_count)DESC – Bruce 2010-04-19 11:50:43

1

SELECT TOP 1 UID (FOLLOWER_COUNT + status_count) 作爲TOTALCOUNT FROM追隨者WHERE p.is_my_friend = 1爲了通過TOTALCOUNT降序

我不是100%,如果該命令是可能的。試試吧,如果沒有創建,結合這些領域