Xana Posted May 25, 2013 Share Posted May 25, 2013 CREATE TABLE `events` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'id', `name` varchar(60) NOT NULL COMMENT 'name', `company` varchar(60) NOT NULL COMMENT 'company', `datetime` datetime NOT NULL COMMENT 'datetime', `url` varchar(160) NOT NULL COMMENT 'announcement pagelink', `anncement_pg` varchar(160) NOT NULL COMMENT 'date the event was announced', `anncement_date` date NOT NULL, PRIMARY KEY (`id`)) ENGINE=MyISAM AUTO_INCREMENT=11 DEFAULT CHARSET=utf8 I'm trying to reproduce these results in a tabular layout that is ordered by ascending dates(datetime). But, I want the first two entries to be the last two recent events, in other words, the last two results in the order before now(), but once I get those results, they are still in descending order. Although they are the two that I need from the table. SELECT * FROM events WHERE (datetime < NOW()) ORDER BY datetime DESC LIMIT 2 This is far as I could get without producing errors. This works for my purposes, except what comes back is descending order, but it's the correct results.How do I order something so that I can get the last two results of that query, but then order it so that it comes back in the array as how I want it to be printed in the list? Or is there an easier way? Edit: MySQL server is 5.1.56 Link to comment https://forums.phpfreaks.com/topic/278387-having-trouble-with-sorted-data/ Share on other sites More sharing options...
kicken Posted May 25, 2013 Share Posted May 25, 2013 Rather than try and get a query to give you exactly what you want, which I'm not sure is possible as the requirements are a bit odd, just order the query in ascending order and then before printing the list, move the last two items to the top. For example, in PHP: $data = $db->query('SELECT * FROM events WHERE (datetime < NOW()) ORDER BY datetime')->fetchAll(); $last2 = array_splice($data, -2); //remove the last two array_unshift($data, $last2[1], $last2[0]); //add them back to the top of the array. Link to comment https://forums.phpfreaks.com/topic/278387-having-trouble-with-sorted-data/#findComment-1432289 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.