aviddv1 Posted October 5, 2006 Share Posted October 5, 2006 I'm trying to order a set of records with a field called "date" so that any records where date is >= now() will show first and be in ascending order. Any records where date< now() will show after the first set and be in descending order.so if today is 10/05/2006 it would look something like this:10/05/200610/15/200610/31/200610/04/20069/28/20069/15/20069/01/2006 Any ideas?Thanks. Link to comment https://forums.phpfreaks.com/topic/23087-ordering-by-date-based-on-today/ Share on other sites More sharing options...
fenway Posted October 5, 2006 Share Posted October 5, 2006 I'm assuming you're storing these in a proper DATE column type... you could probably use "ORDER BY yourDate >= NOW(), yourDate ASC" if you didn't want the direction to change. But since you do, you can't really do this with a single query... not in any way that I can think of right now, at least. Of course, a UNION would do the trick, search you could sort each result set differently. Not sure if that suits your needs. Link to comment https://forums.phpfreaks.com/topic/23087-ordering-by-date-based-on-today/#findComment-104479 Share on other sites More sharing options...
GeoffOs Posted October 5, 2006 Share Posted October 5, 2006 You could write two queries and union the results together[url=http://dev.mysql.com/doc/refman/5.0/en/union.html]http://dev.mysql.com/doc/refman/5.0/en/union.html[/url]For Example[code]select date from my table where date >= now() order by date descUNION ALLselect date from my table where date <= now() order by date asc[/code]Or something like that Link to comment https://forums.phpfreaks.com/topic/23087-ordering-by-date-based-on-today/#findComment-104486 Share on other sites More sharing options...
fenway Posted October 5, 2006 Share Posted October 5, 2006 Strictly speaking, you should only have the equality on one of the queries; also, remember that this won't be query-cached if you use NOW(), and since this will return the same result for 24hr, you may want to pass the current date from you as a string. Just my $0.02. Link to comment https://forums.phpfreaks.com/topic/23087-ordering-by-date-based-on-today/#findComment-104488 Share on other sites More sharing options...
shoz Posted October 5, 2006 Share Posted October 5, 2006 First let me say that I also recommend that you use the UNION. Having said that, this query should also give the same results.[code]SELECTtable.*, IF(date >= '$date', 0, 1) AS d1, IF(date >= '$date', date, 1) AS d2FROMtableORDER BYd1 ASC, d2 ASC, date DESC[/code]EDIT:The following should also work if you're not using dates before 1970.[code]SELECTtable.*, IF(date >= '$date', 0, 1) AS d1,IF(date >= '$date', UNIX_TIMESTAMP(date),0-UNIX_TIMESTAMP(date)) AS d2FROMtableORDER BYd1 ASC, d2 ASC[/code] Link to comment https://forums.phpfreaks.com/topic/23087-ordering-by-date-based-on-today/#findComment-104623 Share on other sites More sharing options...
fenway Posted October 6, 2006 Share Posted October 6, 2006 Interesting workaround... I always seem to ignore the possibility of using such expressions to simplify ordering... and I probably shouldn't. I'm guessing that this single query might be faster, though perhaps a little less obvious. Link to comment https://forums.phpfreaks.com/topic/23087-ordering-by-date-based-on-today/#findComment-104990 Share on other sites More sharing options...
shoz Posted October 6, 2006 Share Posted October 6, 2006 I assumed that the UNION would order the results based on each individual query, but it does not.[quote=http://dev.mysql.com/doc/refman/5.0/en/union.html]Use of ORDER BY for individual SELECT statements implies nothing about the order in which the rows appear in the final result because UNION by default produces an unordered set of rows.[/quote]The manual gives some examples of how you can handle this, but they are similar to the example I posted. You can of course run 2 separate queries, storing the result in order. Link to comment https://forums.phpfreaks.com/topic/23087-ordering-by-date-based-on-today/#findComment-105021 Share on other sites More sharing options...
fenway Posted October 6, 2006 Share Posted October 6, 2006 No kidding... makes sense, I wonder why I've never encountered that before... oh, now I remember. If usually use ORDER BY with LIMIT, so it "works". If you knew how many rows there were, or just used a huge number, you could fake the limit, and I'll bet the order by would still catch. Link to comment https://forums.phpfreaks.com/topic/23087-ordering-by-date-based-on-today/#findComment-105077 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.