shortysbest Posted December 12, 2010 Share Posted December 12, 2010 How can i select the last 3 rows in a table and order it by ASC. When i use DESC it displays the results in the wrong order. $query24 = mysql_query("SELECT * FROM notifications WHERE to_id='$session' AND state='1' ORDER BY id DESC LIMIT 3 "); Quote Link to comment https://forums.phpfreaks.com/topic/221435-mysql-select-last-3-rows-order-by-id-asc/ Share on other sites More sharing options...
MMDE Posted December 12, 2010 Share Posted December 12, 2010 use DESC/ASC whatever you want to and do this... $query24 = mysql_query("SELECT * FROM notifications WHERE to_id='$session' AND state='1' ORDER BY id DESC LIMIT 3 "); $i=mysql_num_rows($query24)-1; while($row=mysql_fetch_array($query24)){ $array[$i]=$row; $i--; } should give you an idea on how to solve it, it's all about finding a nice way to solve the problem. Quote Link to comment https://forums.phpfreaks.com/topic/221435-mysql-select-last-3-rows-order-by-id-asc/#findComment-1146352 Share on other sites More sharing options...
BlueSkyIS Posted December 12, 2010 Share Posted December 12, 2010 or use different SQL: $sql = "SELECT B.* FROM (SELECT A.* FROM notifications A WHERE A.to_id='$session' AND A.state='1' ORDER BY A.id DESC LIMIT 3) AS B ORDER BY B.id ASC"; Quote Link to comment https://forums.phpfreaks.com/topic/221435-mysql-select-last-3-rows-order-by-id-asc/#findComment-1146353 Share on other sites More sharing options...
shortysbest Posted December 12, 2010 Author Share Posted December 12, 2010 Thanks for the responses, Blue, yours works wonderfully. Thanks. MMDE, Thanks for your response, was going to use that method before the last post was made. Appreciate both of your time. Quote Link to comment https://forums.phpfreaks.com/topic/221435-mysql-select-last-3-rows-order-by-id-asc/#findComment-1146355 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.