MySQL; ORDER BY STR_TO_DATE doesn't work vice versa - php

This is my code:
SELECT id, title, date FROM table ORDER BY STR_TO_DATE(date,"%m/%d/%Y %h:%i:%s")
The date output is:
2016-12-11 14:40:00
2016-11-15 08:50:09
2016-11-15 08:54:58
SELECT id, title, date FROM table ORDER BY STR_TO_DATE(date,"%m/%d/%Y %h:%i:%s") DESC
doesn't work for me for some reason.
How can I reach to ORDER BY this?
2016-11-15 08:54:58
2016-11-15 08:50:09
2016-12-11 14:40:00
Edit: date is stored as timestamp in my MySQL database!

Assume you want the data that order by date ascending and time descending.
You can try this:
SELECT id, title, date
FROM table
ORDER BY DATE(date) ASC , TIME(date) DESC

Related

How can I order this MySQL Date?

I have a problem ordering a mysql result by Date. The date is in this format: 15:24:57 - 21/04/2019 (24 hour clock)
I've tried this:
SELECT PlayerSteamID, BanLength, BanReason, AdminName, AdminSteamID, PlayerName, MapName, DateAndHour FROM BansList ORDER BY DateAndHour DESC
and this
SELECT PlayerSteamID, BanLength, BanReason, AdminName, AdminSteamID, PlayerName, MapName, DateAndHour FROM BansList ORDER BY UNIX_TIMESTAMP(STR_TO_DATE(DateAndHour, '%h:%i:%s - %d/%m/%Y')) DESC
But doesn't seem to work...
How can I make it so it's ordered by that date & time?
assuming your DateAndHour column is a string could be you need a proper conversion
SELECT PlayerSteamID
, BanLength
, BanReason
, AdminName
, AdminSteamID
, PlayerName
, MapName
, DateAndHour
FROM BansList
ORDER BY str_to_date(DateAndHour, '%T - %d/%m/%Y') DESC

Select count for each day in a month

I have a sms tracker database with a date column in the format 02/25/2018 04:12:52 pm. I want to count the no of sms sent each day to display it in bar chart.
I could only count sms sent by a user using this query "SELECT count(*) as user_count from table where username = 'CTC01'". How can i get an array of count for each day in a particular month
Since OP's date_column is VARCHAR type. We use STR_TO_DATE function:
SELECT DATE(STR_TO_DATE(date_column, "%m/%d/%Y %r")), COUNT(*)
FROM table
GROUP BY DATE(STR_TO_DATE(date_column, "%m/%d/%Y %r"));
Use DATE function, to convert a datetime expression to a date. Then use GROUP BY to get COUNT datewise.
In case, you want to get data for a specific user (eg: CTC01) and datewise. You can do the following:
SELECT DATE(STR_TO_DATE(date_column, "%m/%d/%Y %r")), COUNT(*)
FROM table
WHERE username = 'CTC01'
GROUP BY DATE(STR_TO_DATE(date_column, "%m/%d/%Y %r"));
I see your date format is 'm/d/Y H:i:s'. So, to get the total for each day in a month, you have to do a comparison against the least time in that month and the highest time in that month. So, the query for February 2018 would be:
SELECT DATE(date_column), COUNT(*)
FROM jobs
where created_on >= '02/01/2018 00:00:00' and created_on < '03/01/2018 00:00:00'
GROUP BY DATE(date_column);
To get for a particular user, simply append a where clause to the query above like so:
SELECT DATE(date_column), COUNT(*)
FROM jobs
where created_on >= '02/01/2018 00:00:00' and created_on < '03/01/2018 00:00:00' and username = 'CTC01'
GROUP BY DATE(date_column);
EDIT
Since your date column is varchar, you first have to convert it to datetime. So run this query instead:
SELECT DATE(DATE_FORMAT(STR_TO_DATE(date_column, '%c/%e/%Y %H:%i'), '%Y-%m-%d %H:%m:%s')) as Date, COUNT(*)
FROM jobs
where created_on >= '02/01/2018 00:00:00' and created_on < '03/01/2018 00:00:00'
GROUP BY DATE(DATE_FORMAT(STR_TO_DATE(date_column, '%c/%e/%Y %H:%i'), '%Y-%m-%d %H:%m:%s'));

Select totals from database table based on month

I have a table which has product quantity column and the database has multiple entries in a single month.
Date format is (YYYY-MM-DD)
Date Quantity
2016-03-01 1200
2016-03-05 200
2016-04-05 500
2016-04-10 1000
2016-05-05 850
2016-05-10 50
So I want data as:
March (2016-03-01 to 2016-03-31) = 1400
April (2016-04-01 to 2016-04-30) = 1500
May (2016-05-01 to 2016-05-31) = 900
How can I do this?
Use the following query and it will return the result that you want.
' date field ' is the name of the column where date is inserted and Tablename is the name of the Table.
SELECT MONTH(date field) as month , YEAR(date field) as year , SUM(quantity) as
quantity FROM Tablename GROUP BY MONTH( date field )
select MONTH(Date) As dt , SUM(quantity) total from #Table1
GROUP BY MONTH(Date)
You can use DATE_FORMAT to convert the date field into just the year-month format, select the SUM of the quantity field, and then GROUP_BY the year-month field.
SELECT DATE_FORMAT('%Y-%m', `date`) as `ym`, SUM(`quantity`) FROM `table` GROUP BY `ym` ORDER BY `ym` ASC
You can try grouping by the month and year, taking the sum of the quantity field as an aggregate.
SELECT MONTH(Date), YEAR(Date), SUM(quantity)
FROM yourTable
GROUP BY MONTH(Date), YEAR(Date)
If you want the fancy output with actual date ranges, that would be a bit more work to do. Those dates may not exist in your original data set, so it could require a date table. And handling February in a leap year could be a real pain.
Use the following query same as it is it will return what you want
SELECT CONCAT(DATE_FORMAT(date,'%b'),' ',
(SELECT CONCAT('(',YEAR(date),'-',LPAD(MONTH(date),2,0),'-01 - ')
), (SELECT concat(last_day(date),')')
)) dates, SUM(quantity) qty FROM DATES GROUP BY MONTH(date) ORDER BY MONTH(date)

Get results order by date in MySQL if date field's datatype is varchar

I want to get results ordered by date from a table in mysql but the problem is the date column's datatype is varhcar
Here is the table structure:
-------------------------------------------
ID EVENT DATE
-------------------------------------------
1 My Wife’s Birthday 23-02-1987
2 Wedding Anniversary 18-05-2006
3 My Son’s Birthday 06-12-2014
4 Ramadan 08-06-2016
-------------------------------------------
INT VARCHAR VARCHAR
Here is the query SELECT event_date FROM events ORDER BY CONVERT(event_date, DATETIME)
Im using CONVERT but I think I'm doing it wrong. Can anyone help?
use STR_TO_DATE() function
order by str_to_date(`DATE`, '%d-%m-%Y')
If we need in ascending order
SELECT * FROM table ORDER by DATE ASC
If we need in descending order
SELECT * FROM table ORDER by DATE DESC

How can I order by date?

I have a problem with a query (mysql).
I have to order by date ASC tha problem is that I also have empty date in the table. Mysql set them to 0000-00-00, so when I order by date those dates are shown first, but i need to put the "empty dates" at the end, how can I do?
Example:
0000-00-00
2011-01-01
2010-12-12
Query: .... ORDER BY date ASC
Results should be:
2010-12-12
2011-01-01
0000-00-00
Thanks
ORDER BY IF(date = '0000-00-00', 1, 0) ASC, date ASC
Assuming you're doing:
SELECT *
FROM table
ORDER BY date ASC
you could do
SELECT *, IF(date > 0, 1, 0) AS has_date
FROM table
ORDER BY has_date ASC, date ASC
I believe that would do it for you.

Categories