Jump to content

Recommended Posts

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

Edited by Xana
Link to comment
https://forums.phpfreaks.com/topic/278387-having-trouble-with-sorted-data/
Share on other sites

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.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.