MySQL Show Events in Logical Order - mysql

looking to organize the following dates (assuming today is August 24, 2012):
December 1, 2012
November 1, 2012
June 1, 2012
June 30, 2012
In the following way:
November 1, 2012
December 1, 2012
June 30, 2012
June 1, 2012
This is such a way that it shows the events that have NOT yet happened first, and from soonest to furthest away, then show past events, from closest to farthest.
You can assume the table structure is:
ID name event_date
1 Test 1351742400 # All dates are Unix Time
2 Test2 1354338000

SELECT ID, name, event_date
FROM yourtable
ORDER BY event_date < UNIX_TIMESTAMP(), ABS(UNIX_TIMESTAMP() - event_date)
See it working online: sqlfiddle

Related

Implement where condition varchar field in MySQL

I have usages table which contains field values like below
id | page_open_time
1, 28 May 2019 08:39:11
2, 12 July 2019 18:39:11
3, 22 December 2019 11:39:11
4, 23 June 2019 12:39:11
Now I would like to get all records where page_open_time is greater than 1 july, 2019. For this, I wrote this query below , but it didnt help. Is there any way, I can perform this ?
SELECT * FROM `usages` where page_open_time > '2019-07-01'
Your dates are not in a valid MySQL DATETIME format, so you need to convert them before comparison, using STR_TO_DATE:
SELECT *
FROM usages
WHERE STR_TO_DATE(page_open_time, '%d %M %Y %H:%i:%s') > '2019-07-01'
Output
id page_open_time
2 12 July 2019 18:39:11
3 22 December 2019 11:39:11
Demo on dbfiddle

How can I sort date perfectly

I was trying to sort the data in-order by date but it doesn't sort perfectly. I don't know if there's an error in the code.
Here's some of the data in character in my database:
March 5, 2016
March 17, 2016
August 9, 2017
April 2, 2016
July 5,2018
January 15, 2019
The query I used is:
SELECT * FROM reporting ORDER BY date ASC
Result are:
April 2, 2016
August 9, 2017
January 15, 2019
July 5,2018
March 5, 2016
March 17, 2016
Expected Result:
March 5, 2016
March 17, 2016
April 2, 2016
August 9, 2017
July 5,2018
January 15, 2019
use str_to_date() to convert your date field from string to datetime
SELECT * FROM reporting ORDER BY str_to_date(`date`,'%M %d, %Y') ASC
use date format
ORDER BY DATE_FORMAT(date, "%F-%d-%Y") as date ASC
You may try this,
For SQL Server:
convert( varchar, [date], 120) will convert your date in yyyy-MM-dd format.
select * from reporting order by convert( varchar, [date], 120) asc
For MySQL:
select * from reporting order by DATE_FORMAT(date, "%Y-%m-%e") asc

How to sort month in calendar order using mysql query

I have a query like this
SELECT Value,month,Year FROM `rainfall_past` WHERE Year = '2006' and District= 'Colombo'
this will return the following results
Value month Year
103.4 April 2006
270.6 August 2006
51.9 December 2006
156.9 February 2006
126.9 January 2006
96.8 July 2006
183.1 June 2006
266.6 March 2006
193.1 May 2006
524.7 November 2006
619.9 October 2006
129 September 2006
the problem is that as u can see the months are not in the calendar order. how can query this in calendar months order. i don't want to add additional column to the table or write a script in my source code to sort this. is there any way i can do this in my query only. thanks guys for ur support
It seems like month is a text field. If this is the case, then you can use FIELD function in the ORDER BY clause:
SELECT Value, month, Year
FROM `rainfall_past`
WHERE Year = '2006' and District= 'Colombo'
ORDER BY Year,
FIELD(month, 'January', 'February', ..., 'December')
If, on the other hand, month is deduced from a datetime field, you can use MONTH(date).

Order By before a Group By [duplicate]

This question already has answers here:
GROUP_CONCAT ORDER BY
(5 answers)
Closed 8 years ago.
I need to pull some data on a couple thousand records, but I'm having trouble sorting the results.
SELECT IdCode, GROUP_CONCAT(' ',CONCAT(MONTHNAME(ei.StartDatetime),' ',YEAR(ei.StartDatetime)))
FROM exclusion_import_data eid JOIN exclusion_import ei ON ei.ImportId = eid.ImportId
WHERE ( IdCode IN ( '84277', '253533' ))
GROUP BY IdCode
ORDER BY RecordId ASC;
Basically I'm trying to see which months specific records appeared in our system.
253533 May 2013, November 2013, December 2013, January 2014, February
2014, March 2014, April 2014, May 2014, June 2014, August 2014,
October 2013, September 2013, June 2013, July 2013, May 2013, August
2013 84277 September 2013, April 2014, May 2013, May 2014, June 2014,
May 2013, March 2014, June 2013, February 2014, January 2014, July
2013, December 2013, November 2013, August 2013, October 2013, August
2014
The above query returns the correct months, separated by the idCodes, but the months are all out of order.
How can I sort the records before grouping them?
Use the order by option in the group_concat():
SELECT IdCode,
GROUP_CONCAT(' ', CONCAT(MONTHNAME(ei.StartDatetime),' ',YEAR(ei.StartDatetime))
ORDER BY ei.StartDatetime)
FROM exclusion_import_data eid JOIN
exclusion_import ei
ON ei.ImportId = eid.ImportId
WHERE ( IdCode IN ( '84277', '253533' ))
GROUP BY IdCode
ORDER BY RecordId ASC;
You can specify an ordering within the GROUP_CONCAT function, for example:
GROUP_CONCAT(DATE_FORMAT(ei.StartDatetime,'%M %Y') ORDER BY ei.StartDatetime)
^^^^^^^^^^^^^^^^^^^^^^^^^
(My preference would be to get the month and year with a single function call, and one reference to the column, rather than three function calls and two references to the same column. I've demonstrated that above as well.)
Ref: http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat

MySql Query- Date Range within a Date Range

I use mySql 5 and IIS.
I have products, that have a start date field and an end date field.
I need to run a query that will take user entered Start and End dates, and output the number of days that the product ran within the date range.
Example:
Offer1 - July 1 2011 thru July 31 2011
Query - July 1 2011 thru Sept 15 2011
Results = 31
Example:
Offer1 - July 1 2011 thru July 31 2011
Query - July 1 2011 thru July 15 2011
Results = 15
If your products have a start_date and an end_date and your query has a qstart_date and a qend_date, then we want the number of days between:
GREATEST(start_date, qstart_date)
and
LEAST(end_date,qend_date)
. In MySQL I think this looks like
1 + DATEDIFF ( 'd' , GREATEST(start_date, qstart_date) , LEAST(end_date,qend_date) )
And you'll want to ignore negative numbers, replacing them with "0".