如何在特定值后过滤结果

时间:2012-12-02 16:07:44

标签: mysql

SELECT user_profile_data.*
FROM relation_table,user_profile_data
WHERE user_profile_data.ref_user_id=relation_table.user2
and relation_table.type = 1
AND relation_table.user1 = 184
ORDER BY user_profile_data.full_name ASC

我有这个查询产生这里显示的结果表: - http://sqlfiddle.com/#!2/340f0/1

FULL_NAME   LOCATION    REF_USER_ID
Fake        IN          181
Fake gupta  IN          185
Fake kumar  IN          182
Fake sharma IN          183
Fakeme  IN              180
Manish Kumawat US       192

现在我想修改我的查询,以便得到'假Kumar'之后的结果,其id = 182,如下所示: -

FULL_NAME   LOCATION    REF_USER_ID

Fake sharma IN          183
Fakeme  IN              180
Manish Kumawat US       192

我无法使用user_profile_data.full_name>'Fake Kumar',因为一些安全问题,我可以使用的是id,但如果我使用user_profile_data.ref_user_id>182,我会得到如下结果: -

FULL_NAME   LOCATION    REF_USER_ID

Fake gupta  IN          185

Fake sharma IN          183

Manish Kumawat US       192

我应该使用什么查询来获得所需的结果

2 个答案:

答案 0 :(得分:1)

SELECT   user_profile_data.*
FROM     relation_table
    JOIN user_profile_data
      ON user_profile_data.ref_user_id = relation_table.user2
WHERE    relation_table.type  = 1
     AND relation_table.user1 = 184
     AND user_profile_data.full_name > (
       SELECT full_name
       FROM   user_profile_data
       WHERE  REF_USER_ID = 182
     )
ORDER BY user_profile_data.full_name

sqlfiddle上查看。

答案 1 :(得分:0)

使用LIMIT,如下所示:

      SELECT user_profile_data.*
      FROM relation_table,user_profile_data
      WHERE user_profile_data.ref_user_id=relation_table.user2
            and relation_table.type = 1 
            AND relation_table.user1 = 184
      ORDER BY user_profile_data.full_name ASC
      LIMIT 3,3