locked
combine this 2 simple querys into 1 query for performance. RRS feed

  • Question

  • User1574220431 posted

    hi friends.
    first of all - sorry on my english ;-).
    i have table calld users with the following records :

    user_id         user_name
    1                  alex
    2                  robert
    3                  sam
    4                  mike
    5                  jhon
    6                  kevin
    7                  blink
    8                  jek
    9                  stefeni

    i want to write query that give me the 5 next users of specific user , for example :
    "select * from users where user_id>2 order by user_id limit 5" give you user_id 3,4,5,6,7 - its work fine when the user_id not in the bottom of the table (like id:6,7,8,9)
    and hence when the user_id in the bottom i need always to check if i get 5 users, if not i select the rest of the users from the start, circular way.
    for example: the five users that come after user_id=8 are: 9,1,2,3,4

    today i'm doing thats with 2 query, anyone think that can be done with one query...?
    your help is welcome.

    Tuesday, September 13, 2011 7:57 AM

Answers

  • User1574220431 posted

    i find the solution whith UNION funtion  !
    thank for all helpers !! 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 13, 2011 9:19 AM

All replies

  • User-126497635 posted

    SELECT TOP 5 * FROM Users WHERE User_ID > 2

    Tuesday, September 13, 2011 8:43 AM
  • User1574220431 posted

    top function dosent work on mysql only mssql - any other idea ?
    or did you know how implement it on mysql .. ? 

    Tuesday, September 13, 2011 8:51 AM
  • User1574220431 posted

    i find the solution whith UNION funtion  !
    thank for all helpers !! 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 13, 2011 9:19 AM