![]() | |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Hello! I have got a tabale with dates: 2006-12-01 2007-01-12 2006-12-31 2008-01-05 2006-12-08 I want to display ALL dates, but sort this way, that the closest dates are on top, next there are future dates, and at the end are past dates: 2007-01-12 2008-01-05 2006-12-31 2006-12-08 2006-12-01 How do this? Should I create temporary table and load dates with my conditons or do something else? Does solution with temporary table is efficient? |
#3
| |||
| |||
|
|
krzys wrote: Hello! I have got a tabale with dates: 2006-12-01 2007-01-12 2006-12-31 2008-01-05 2006-12-08 I want to display ALL dates, but sort this way, that the closest dates are on top, next there are future dates, and at the end are past dates: 2007-01-12 2008-01-05 2006-12-31 2006-12-08 2006-12-01 How do this? Should I create temporary table and load dates with my conditons or do something else? Does solution with temporary table is efficient? surely "closest" dates are also future dates?? And is today a closest date, a past date or a future date? Assuming that the date field is called date_field and the table is called date_table, try: SELECT date_field FROM date_table WHERE date_field > NOW() ORDER BY date_field UNION SELECT date_field FROM date_table WHERE date_field <= NOW() ORDER BY date_field DESC |
#4
| ||||
| ||||
|
|
Hello! I have got a tabale with dates: 2006-12-01 2007-01-12 2006-12-31 2008-01-05 2006-12-08 I want to display ALL dates, but sort this way, that the closest dates are on top, next there are future dates, and at the end are past dates: 2007-01-12 2008-01-05 2006-12-31 2006-12-08 2006-12-01 How do this? Should I create temporary table and load dates with my conditons or do something else? Does solution with temporary table is efficient? |
. If there are secondary keys, you could get closer,|
Field | Type | Null | Key | Default | Extra | +-------+-----------+------+-----+-------------------+-------+ a | timestamp | YES | | CURRENT_TIMESTAMP | | b | date | YES | | NULL | | c | time | YES | | NULL | | +-------+-----------+------+-----+-------------------+-------+ |
|
a | b | c | +---------------------+------------+----------+ 2007-01-06 12:30:04 | 2007-01-06 | 12:30:04 | +---------------------+------------+----------+ |
|
UNIX_TIMESTAMP(a) | UNIX_TIMESTAMP(b) | UNIX_TIMESTAMP(FROM_UNIXTIME(c)) | +-------------------+-------------------+----------------------------------+ 1168108204 | 1168063200 | 123004 | +-------------------+-------------------+----------------------------------+ |
#5
| |||
| |||
|
|
krzys wrote: Hello! I have got a tabale with dates: 2006-12-01 2007-01-12 2006-12-31 2008-01-05 2006-12-08 I want to display ALL dates, but sort this way, that the closest dates are on top, next there are future dates, and at the end are past dates: 2007-01-12 2008-01-05 2006-12-31 2006-12-08 2006-12-01 How do this? Should I create temporary table and load dates with my conditons or do something else? Does solution with temporary table is efficient? First, it would be interesting to note the business case for needing data presented in this manner - in generic terms of course... no need for details... ORDER BY - which uses a simple sort-routine can only do ascending or decending - not mix-cending . If there are secondary keys, youcould get closer, depending on that key value - such as another date. Even that will still only do ascending/descending. If these dates could in some way be transformed into Spatial data, then you would have a way to find "points" that are further and further away from the starting point. This is used on some sites to find "stores" near your current zip code and can order them based on distance. The link below would appear to be an excellent starting point - you will need to think WAY outside the box. Don't forget - in your spatial data table, you would need a column that is the "date" that can be joined to the other table. http://dev.mysql.com/tech-resources/...ith-mysql.html This may help you get started... mysql> describe mm; +-------+-----------+------+-----+-------------------+-------+ Field | Type | Null | Key | Default | Extra | +-------+-----------+------+-----+-------------------+-------+ a | timestamp | YES | | CURRENT_TIMESTAMP | | b | date | YES | | NULL | | c | time | YES | | NULL | | +-------+-----------+------+-----+-------------------+-------+ 3 rows in set (3.93 sec) mysql> insert into mm values (current_timestamp,current_date,current_time); Query OK, 1 row affected (0.21 sec) mysql> select * from mm; +---------------------+------------+----------+ a | b | c | +---------------------+------------+----------+ 2007-01-06 12:30:04 | 2007-01-06 | 12:30:04 | +---------------------+------------+----------+ 1 row in set (0.01 sec) mysql> select UNIX_TIMESTAMP(a), UNIX_TIMESTAMP(b), UNIX_TIMESTAMP(FROM_UNIXTIME (c)) from mm; +-------------------+-------------------+----------------------------------+ UNIX_TIMESTAMP(a) | UNIX_TIMESTAMP(b) | UNIX_TIMESTAMP(FROM_UNIXTIME(c)) | +-------------------+-------------------+----------------------------------+ 1168108204 | 1168063200 | 123004 | +-------------------+-------------------+----------------------------------+ 1 row in set (0.02 sec) If stored "spatially", then you can calculate "distance" between two dates and sort by that "distance" - but of course, you would then only display the dates. |
#6
| |||
| |||
|
|
Michael Austin wrote: krzys wrote: Hello! I have got a tabale with dates: 2006-12-01 2007-01-12 2006-12-31 2008-01-05 2006-12-08 I want to display ALL dates, but sort this way, that the closest dates are on top, next there are future dates, and at the end are past dates: 2007-01-12 2008-01-05 2006-12-31 2006-12-08 2006-12-01 [snipped for brevity] |
|
a | b | c | +---------------------+------------+------+ 2007-01-06 15:01:47 | 2006-12-31 | NULL | 2007-01-06 15:00:37 | 2007-01-12 | NULL | 2007-01-06 15:01:47 | 2007-01-12 | NULL | 2007-01-06 15:01:47 | 2008-01-05 | NULL | 2007-01-06 15:01:47 | 2006-12-01 | NULL | 2007-01-06 15:01:47 | 2006-12-08 | NULL | +---------------------+------------+------+ |
![]() |
| Thread Tools | |
| Display Modes | |
| |