Ordering MYSQL by date then time - mysql

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?

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

mySQL ORDER BY TODAY AND THEN NORMAL SORTING

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

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.

How to get the average price for the X most recent rows based on date?

I am looking to calculate moving averages over variable dates.
My database is structured:
id int
date date
price decimal
For example, I'd like to find out if the average price going back 19 days ever gets greater than the average price going back 40 days within the past 5 days. Each of those time periods is variable.
What I am getting stuck on is selecting a specific number of rows for subquery.
Select * from table
order by date
LIMIT 0 , 19
Knowing that there will only be 1 input per day, can I use the above as a subquery? After that the problem seems trivial....
if you only have one input per day you don't need id, date can be your primary id? Am i missing something? Then use select sum
SELECT SUM(price) AS totalPrice FROM table Order by date desc Limit (most recent date),(furthest back date)
totalPrice/(total days)
I may not understand your question
Yes you can use that as a sub-query like this:
SELECT
AVG(price)
FROM
(SELECT * FROM t ORDER BY date DESC LIMIT 10) AS t1;
This calculates the average price for the latest 10 rows.
see fiddle.

Pull events from database in order from today's date on

I am building a community site for my fire department and they want to show upcoming events but I don't want to show events that have already passed so here is what I have ( I am using dreamweaver btw).
Database with tables (table = events1532):
-id (int)
-event_start (datetime) YEAR-MO-DA HR:MI:SE
-event_end (datetime) YEAR-MO-DA HR:MI:SE
-event_location (text)
-event_desc (varchar)
I need to get the event start date from the soonest event that has not passed for the front page but I need it in this format = 03/05/2013 at 12:00 AM.
Any ideas guys?
You can use the CONCAT and DATE_FORMAT functions in order to manipulate the result as you want it.
SELECT CONCAT(DATE_FORMAT(event_start, '%d/%m/%Y'), " at ", DATE_FORMAT(event_start, '%h:%m %p')), event_location, event_desc
FROM events1532
WHERE event_start > NOW()
ORDER BY event_start ASC
LIMIT 1;
MySQL Date Format Options
The NOW() function returns a datetime of the current time. You can test this in a MySQL query by simply doing:
SELECT NOW();
The rest is basically ordering the results in ascending order (meaning the soonest date that is greater than NOW() will be at the top of the results), and limiting the result to 1. You could of course limit it to more depending on how many you want returned.