cmor Posted August 1, 2007 Share Posted August 1, 2007 Why oh why does this not work: @ $db = new mysqli('localhost', user', 'password', 'database'); $query = "select * from weather where timekey in (select * from weather ORDER BY timekey desc LIMIT 168) order by timekey asc"; $result = $db->query($query); I need to select the last records in the database and order them in ascending order. sigh.... Quote Link to comment https://forums.phpfreaks.com/topic/62896-select-last-100-records-inserted/ Share on other sites More sharing options...
akitchin Posted August 1, 2007 Share Posted August 1, 2007 the reason it doesn't work is because you're grabbing all 168 of the records in descending order for the sub-query, which the overall select query is then just reshuffling. you need to limit the subquery to 100, or you can just toss them in an array and resort using PHP: while ($row = mysql_fetch_assoc($resource)) { $stuff[] = $row; } $new_order = array_reverse($stuff); Quote Link to comment https://forums.phpfreaks.com/topic/62896-select-last-100-records-inserted/#findComment-313099 Share on other sites More sharing options...
cmor Posted August 1, 2007 Author Share Posted August 1, 2007 I actually want 168 records.....I guess I spaced on the post title. There is currently a 1000 or so in the database The currentlly produces: nothing Thanks for the response Quote Link to comment https://forums.phpfreaks.com/topic/62896-select-last-100-records-inserted/#findComment-313105 Share on other sites More sharing options...
akitchin Posted August 1, 2007 Share Posted August 1, 2007 it's possible that your version of MySQL doesn't support sub-queries. have you tried adding an or die() statement to your mysql_query() line to see if it's a SQL syntax error? Quote Link to comment https://forums.phpfreaks.com/topic/62896-select-last-100-records-inserted/#findComment-313106 Share on other sites More sharing options...
Barand Posted August 3, 2007 Share Posted August 3, 2007 if that is the case (versions below 4.1, I think) then <?php mysql_query ("CREATE TEMPORARY TABLE tmp SELECT timekey FROM WEATHER ORDER BY timekey DESC LIMIT 168"); $res = mysql_query ("SELECT w.* FROM WEATHER w INNER JOIN tmp t ON w.timekey = t.timekey ORDER BY timekey") ?> Quote Link to comment https://forums.phpfreaks.com/topic/62896-select-last-100-records-inserted/#findComment-314794 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.