2010-04-19 156 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%,如果该命令是可能的。试试吧,如果没有创建,结合这些领域