mySQL ORDER BY TODAY AND THEN NORMAL SORTING - mysql

How can I filter the dates that it would sort first by today then normal?
I have a column with data type datetime, I wanted my results to be sorted showing today's date first and continue normal sorting.

What about
SELECT
...
FROM
...
ORDER BY IF(DATE(datefield=CURRENT_DATE()),0,1), datefield DESC
Edit
Added the DESC to the ORDER BY after the 3rd comment to the OQ

Related

Get first date smaller than given date

I am storing several dates in a MySQL database.
1992-01-03
1990-02-30
1990-01-28
1990-01-13
1990-01-01
(Note: The order of the dates is not the same as the order in my database)
If I referenced the date 1990-01-29 for any arbitrary reason, and I needed to get the first date that was smaller than 1990-01-29, how could I create a query that would do that for me?
Search for all dates that are less than the one you want, sort the result and just grab one row. Something like:
SELECT theDate
FROM yourTable
WHERE theDate < '1990-01-29'
ORDER BY theDate DESC
LIMIT 1

Ordering MYSQL by date then time

I am trying to order MYSQL by date then time.
SELECT START_DATE FROM Table ORDER BY DATE(START_DATE) DESC, TIME(START_DATE) ASC
so what i am trying to do is to get all entries ordering by latest date then order those entries by earliest time.
Exampe:
2016-11-6 6:45:00
2016-11-6 6:30:00
2016-11-6 6:15:00
2016-11-5 6:30:00
2016-11-4 6:30:00
2016-11-4 6:15:00
i want to have the data ordered like the above example but, i then want to have 2016-11-4 6:15:00 on top of 6:30:00. any way to do this?

MySQL Date Format For Ordering By Date

What is the best way to format a date in a MySQL query?
I am currently running the query below which selects the data but doesn't order the information correctly, I presume this is due to the current format of the 'updatetime' row.
SELECT * FROM updates WHERE udcode='Remote Connection' ORDER BY updatetime DESC LIMIT 20;
The current format is as follows:
31/03/2015 13:41:45
How should this date be formatted in order for the ORDERING to work correctly?
Thanks in advance.
Use:
ORDER BY DATE_FORMAT(updatetime, '%Y-%m-%d %H:%i:%S') DESC
you can change the format of your date in MySQL with DATE_FORMAT
SELECT *, DATE_FORMAT(`updatetime`, '%Y-%m-%d %H:%i:%S') AS mydate FROM updates WHERE udcode='Remote Connection' ORDER BY mydate DESC LIMIT 20;
http://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_date-format
You are doing this correctly in the Query, however if you are getting undesired results then there is a possibility that the data type for your updatetime attribute is not correct.
You should use datetime as the data type so that the query is correctly able to distinguish that the information is a date and knows how to order it properly. For example if you are using a varchar it will think that the first digit reading left to right is the most significant rather than recognising the difference and relationship between days, months, years and time.
If you dont have the correct datatype you would either have to include formatting in a bloated query or you would end up with all the days from 10-19 ordered before the 20-29 and then followed by the days 3-9. In this situation the 30th would be considered to be ordered before the 4th for example.
There will be little relevance by the time you are ordering months or years as the day units will have mixed everything up
You can try following changes
Change date format to
yyyy-mm-dd hh:mm:ss
And edit query to
SELECT * FROM updates WHERE udcode='Remote Connection' ORDER BY `updatetime` DESC LIMIT 0, 20;

MySQL sort on year/month/day

I have a large list of dates of the format dd-mm-yyyy.
So I want to order on: year, then on month and then on day.
Date and month are in the same field and year is an other field.
I have now: ORDER BY table.year ASC, table.date ASC
The result is that the list is order on year and then days.
How to split/strip the dd-mm format and first sort on month before sorting on days?
Same record:
date | year
dd-mm | yyyy
based on your example you can sort the record like this,
ORDER BY STR_TO_DATE(CONCAT(year, '-', date), '%Y-%d-%m') ASC
SQLFiddle Demo
As per my knowledge it's better to use single date type field instead of having seperate two fields for date-month and year as you can easily sort your results.
According to your query date-month can be stripped out using RIGHT(date-monthfield, 2) function. This selects month which is on the right side.
The query would be:
select RIGHT(date-monthfield, 2) from table ORDER BY date-monthfield ASC;
Hope it helps.

ORDER BY two colums, but not sorting by time?

I've posted this question before and found a solution: ORDER BY with two columns in MySQL
It's about sorting events. The priority SHOULD BE that TOP-PREMIUM and PREMIUM events are always on top of each date, no matter what.
The solution was that one column was ENUM and not INT. That's why he sorted wrong.
So now I'm sorting this way: ORDER BY e.date ASC, e.highlight DESC
Now I've another new problem, because all my dates were set to time 00:00:00 the time I asked the question here and thought it's all solved. If I'm setting the time to some hour after 00:00, it will be at the top of all events. Even in top of PREMIUM AND TOP-PREMIUM (highlight) events.
Can I somehow let MySQL ignore the time of the datetime type column date or is there any other way to make this work like I want to?
The problem also is that I can't order by highlight DESC first, because then the whole list won't be sorted by date.
You can use the CAST() function or the DATE_FORMAT function to get the date portion only, so this should work:
ORDER BY CAST(e.date AS DATE), e.highlight DESC
ORDER BY DATE_FORMAT(e.date, '%Y-%m-%d'), e.highlight DESC
Demo: SQL Fiddle
Edit: Updated sql fiddle demo to use 2 columns, you can see that the time portion of the date is not affecting sort order.
You can also use the DATE function, which strips the time component from a DATETIME:
ORDER BY DATE(e.date), e.highlight DESC