Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

SQL Query - return last n records

  • 20-11-2006 2:02pm
    #1
    Registered Users, Registered Users 2 Posts: 1,127 ✭✭✭


    This is probably staring me straight in the face. Im using MySQL to power an online football league, and I need a SQL statement to return last 3 records from database (to show a snapshot of the relegation zone muah muah muah muah :D )

    Anyone know the statement? Dont even know where to start. Got the leaders one alright. (SELECT * FROM players ORDER BY points ASC LIMIT 3) straightforward enough..

    Help!


Comments

  • Registered Users, Registered Users 2 Posts: 32,136 ✭✭✭✭is_that_so


    This is probably staring me straight in the face. Im using MySQL to power an online football league, and I need a SQL statement to return last 3 records from database (to show a snapshot of the relegation zone muah muah muah muah :D )

    Anyone know the statement? Dont even know where to start. Got the leaders one alright. (SELECT * FROM players ORDER BY points ASC LIMIT 3) straightforward enough..

    Help!
    SELECT * FROM players ORDER BY points DESC LIMIT 3 ?


  • Registered Users, Registered Users 2 Posts: 1,127 ✭✭✭smcelhinney


    I thought the same. But it then starts with the bottom record, then second-last, then third last.

    Which kinda gives the wrong message in a relegation dogfight.. : )


  • Registered Users, Registered Users 2 Posts: 7,468 ✭✭✭Evil Phil


    SELECT * FROM players ORDER BY points DESC LIMIT 3

    and just reverse the result set in memory. What language is the site developed in? I doubt its difficult.


  • Closed Accounts Posts: 169 ✭✭akari no ryu


    Then just reverse the returned array, surely.


  • Registered Users, Registered Users 2 Posts: 2,494 ✭✭✭kayos


    Totally a MS SQL answer but meh
    SELECT 
    	D.* 
    FROM 
    	(
    	SELECT TOP 3 
    		T.* 
    	FROM 
    		<Table> AS T 
    	ORDER BY 
    		T.Points ASC
    	) AS D 
    ORDER BY 
    	D.Points DESC
    

    That will reverse the order for ya.

    Really dont use MySQL so dont know if you can do anything like that. I'm guessing no as the only person I know that used MySQL recently started on MS SQL and thought the above feature was the best thing since sliced bread.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 1,127 ✭✭✭smcelhinney


    Ya, could do that. But am using mixture of JNDI/JSTL, and dont want scriptlets all over the page.

    Would also like to do it in SQL, as it means I can move all SQL queries to stored procedures when JSTL finally catches up with MySQL 5.0.

    Will keep looking, presumably its sub-queries. I dont know any MySQL functions that do it off hand.

    If anyone gets an epiphany in the meantime, greatly appreciate it.

    Cheers
    Stephen


Advertisement