poe Posted November 17, 2006 Share Posted November 17, 2006 i want to sort a column and show the 4 largest numbers from smallest to largest.i know if i " ORDER BY col DESC LIMIT 4 " that will get me the highest 4 numbers. but i now want to sort these numbers from small to largerandom order:38 : 22 : 19 : 45 : 15 : 02 : 78ORDER BY col DESC LIMIT 4:78 : 45 : 38 : 22 [excluded 19, 15, 02]i want to show:22 : 38 : 45 : 78if i make another 'ORDER BY' it gives me an error Quote Link to comment Share on other sites More sharing options...
fenway Posted November 17, 2006 Share Posted November 17, 2006 Try this (UNTESTED):[code]SELECT col FROM ( SELECT col FROM yourTable ORDER BY col DESC LIMIT 4 ) ORDER BY col[/code] Quote Link to comment Share on other sites More sharing options...
poe Posted November 18, 2006 Author Share Posted November 18, 2006 i get this errormysql_query() failed: Every derived table must have its own alias Quote Link to comment Share on other sites More sharing options...
printf Posted November 18, 2006 Share Posted November 18, 2006 A database is very logical, if you ask a question then it will give you an answer, but if you try to ask (2) questions at once and expect (1) answer it will never work. So ask 1 (question) < enclose the first question, then give it the second question![code]$sql = "(SELECT id FROM table ORDER BY id DESC LIMIT 4) ORDER BY id ASC";[/code]printf Quote Link to comment Share on other sites More sharing options...
fenway Posted November 19, 2006 Share Posted November 19, 2006 Didn't know you could do that... but I did miss an alias in my previous code. I wonder if there's a difference in execution time. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.