需要有关SQL查询的帮助

时间:2010-04-19 11:39:50

标签: 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)
    )";

我需要找到max(status_count + follower_count)且is_my_friend = 1

的人的uid

我编写了以下查询,但我没有得到正确的uid。

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

3 个答案:

答案 0 :(得分:3)

以下查询将起作用:

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

限制0,1适用于MySql。

或者,如果您想要返回仅跟随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 :(得分:1)

SECLECT uid FROM followers ORDER BY (follower_count + status_count) DESC WHERE is_my_friend = 1 

答案 2 :(得分:1)

  

SELECT top 1 uid   (FOLLOWER_COUNT + status_count)   ascount FROM FROM followers WHERE   p.is_my_friend = 1个按totalcount desc排序

如果订单可能,我不是100%。尝试一下,如果没有创建一个组合这些字段的视图