views:

23

answers:

2

I have a list of users, there is a column 'points' and I need to create a list of the ten people with lower points and the ten people with higher points than the user.

The only trouble is I have no way of knowing at this stage what their points will be so I can't use hard values.

Help? Thanks in advance :)

A: 
SELECT * FROM users ORDER BY points ASC LIMIT 10 UNION
SELECT * FROM users ORDER BY points DESC LIMIT 10
Codler
This one will not work as far as where clause is empty. Also it does not resolve the problem about specified user points number that question author presented.
ŁukaszW.pl
UNION ALL should be used here as well, not just UNION
nos
lord I must seem rude - thank you gentlemen, thank you very much indeed.
Jacaranda
+1  A: 

Try this for more points then him

select * from users u
         where u.points > (select u2.points from users u2 where u2.id = ID_OF_THIS_USER)
         order by u.points ascending
         limit 10

and this for less points then him

select * from users u
         where u.points < (select u2.points from users u2 where u2.id = ID_OF_THIS_USER)
         order by u.points descending
         limit 10        

As you can see you are getting specified user points number by subquery.

ŁukaszW.pl
Don't forget to accept this answer if it helps you ;)
ŁukaszW.pl