1

i have one table named users. In this table i have one column named credits(not unique). Now i want the second highest user accroding to the users credits. If the credits field is unique thenmy below query is working fine

SELECT * FROM users ORDER BY credits DESC LIMIT 1 , 1

But , if the users credit is not unique then its create problem for retrive me data suppose,

mack has 200 credits
jack has 200 credits
rock has 150 credits

when i has this types of record then,in output of this query i want the rock record not jack

can anyone help me to find out the correct value ?

thanks in advance

4

3 回答 3

1
SELECT  a.*
FROM    users a
        INNER JOIN
        (
            SELECT DISTINCT credits
            FROM users
            ORDER BY credits desc
            LIMIT 1,1
        ) b ON a.credits = b.credits
于 2012-11-16T05:30:22.687 回答
1

希望这会有所帮助(首先获取second highest credits然后users having those that credit and从顶部找到选择一个`。这将检索一个具有第二高信用的用户):

     SELECT * FROM users
     WHERE credits = (SELECT distinct credits FROM users 
                   ORDER BY credits DESC LIMIT 1,1)
     LIMIT 1;

编辑:如果您还想在具有相同分数的用户中进行选择,则使用适当的过滤器/排序条件,例如在 and 之间进行选择,rock您可以根据名称进行另一个排序(假设名称是具有名称的列)rockjenni

     SELECT * FROM users
     WHERE credits = (SELECT distinct credits FROM users 
                   ORDER BY credits DESC LIMIT 1,1)
     ORDER name desc 
     LIMIT 1;

要同时获得rockand jenni,只需从末尾删除限制并更新内部限制,例如:

     SELECT * FROM users
     WHERE credits = (SELECT distinct credits FROM users 
                   ORDER BY credits DESC LIMIT 1, 1);
于 2012-11-16T05:28:01.170 回答
0

尝试这个

Select * FROM users 
Where Credits < (Select Max(Credits) From Users)
ORDER BY credits DESC LIMIT 1;
于 2012-11-16T05:31:08.637 回答