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 Link to comment https://forums.phpfreaks.com/topic/27546-sort-results/ 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] Link to comment https://forums.phpfreaks.com/topic/27546-sort-results/#findComment-126068 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 Link to comment https://forums.phpfreaks.com/topic/27546-sort-results/#findComment-126636 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 Link to comment https://forums.phpfreaks.com/topic/27546-sort-results/#findComment-126666 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. Link to comment https://forums.phpfreaks.com/topic/27546-sort-results/#findComment-127188 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.