How to order date, like this - 2012-02-01 00:00:00 by the hour,minutes,and the seconds, not by the year/moth/day.
If i have..
2012-02-01 02:00:00
2012-03-01 20:00:00
2012-04-01 12:00:00
2012-05-01 07:00:00
I wan't to get this output.
Column tipe is timestamp.
2012-02-01 02:00:00
2012-05-01 07:00:00
2012-04-01 12:00:00
2012-03-01 20:00:00
ORDER BY TIME(date_column)
This will, however, slow down your queries, as it isn't possible to index the on-the-fly calculation. If you have a lot of records, or if this query runs frequently, you should break the time portion of the date into its own column so you can index it for faster sorting.
Use the TIME() function to extract the time portion of the expression passed, e.g.
mysql> SELECT TIME('2012-02-01 02:00:00');
-> '02:00:00'
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_time
Related
What I have:
A MySQL table consisting of a "duration" column.
05:00:00
00:30:00
00:30:00
00:15:00
01:45:00
00:30:00
01:30:00
02:30:00
01:00:00
01:30:00
Data Type:
I first used TIME but from what I've researched, TIME is for storing a point in time rather than a duration of time. TIME also represents a limitation in that it can't exceed 23 hours.
I've instead opted for the DATETIME data type. My column now looks as follows:
0000-00-00 05:00:00
0000-00-00 00:30:00
0000-00-00 00:30:00
0000-00-00 00:15:00
0000-00-00 01:45:00
0000-00-00 00:30:00
0000-00-00 01:30:00
0000-00-00 02:30:00
0000-00-00 01:00:00
0000-00-00 01:30:00
Sum:
I need the total duration. I.e. SUM of the "duration" column in HH:MM:SS.
Summing the column produces "134000".
SELECT sum(`duration`) FROM `my_table`
My question:
How do I sum the duration column in a HH:MM:SS format?
SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(`duration`)));
TIME_TO_SEC - will convert datetime to seconds. Then the SUM in your case will be 54000
SEC_TO_TIME - convert it back to datetime format
Output
15:00:00
I have this table
FLIGHTS
FNO Departs Arrives Price
111 10:00:00 11:30:00 5000
222 13:30:00 18:00:00 6000
333 20:00:00 22:30:00 3000
444 22:45:00 23:30:00 1000
Requirement:
I want to calculate the TOTAL_TIME of Travel using only flights 111,222,333.
I tried TIMEDIFF and ADDTIME, but I am not able to add results of subquery and get in TIME format.
Try this:
SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(TIMEDIFF(Arrives, Departs)))) Total_Duration
FROM FLIGHTS
WHERE FNO IN (111,222,333);
See DEMO on SQL Fiddle.
I am using Mysqli
How can I do a single query that filters a subquery result? Is it possible?
I need to find users from a subquery result in a particular timestamp period.
SELECT * FROM Table WHERE (using subquery results i.e SELECT ...) BETWEEN 2017-01-08 09:00:00 AND 2017-01-08 12:00:00.
I should get back Mr A and Mr B.
Subquery results:
TimeIn User TimeOut
2017-01-06 10:00:00 Mr A 2017-01-08 11:00:00
2017-01-07 10:00:00 Mr A 2017-01-08 12:00:00
2017-01-08 10:00:00 Mr A 2017-01-08 13:00:00
2017-01-08 09:00:00 Mr B 2017-01-08 11:00:00
Thank you for your kind help as I am quite lost on how to do this.
Like this
SELECT * FROM
(
select ...
) subquery_alias_name
where timeIn BETWEEN '2017-01-08 09:00:00' AND '2017-01-08 12:00:00'
Don't forget to give the subquery an alias name
Let's say I have a date 2013-03-01 and date 2013-04-02. How do I get a integer value between that date, for example in this case 2 days. In SQL kindly advise how to construct the query
day_date
---------------------
2005-07-29 00:00:00
2013-03-01 00:00:00
2013-04-02 00:00:00
2013-06-01 00:00:00
2013-10-19 00:00:00
2013-10-23 00:00:00
2013-12-31 00:00:00
The DATEDIFF() function returns the time between two dates.
SELECT DATEDIFF('2014-11-30','2014-11-29') AS DiffDate
I have a table called eventdata like this
Timestamp GUId EventTypeId
2015-02-01 05:00:00 1100 E012-1
2015-02-01 05:30:00 1100 E012-2
2015-02-01 06:00:00 1100 E012-1
2015-02-01 06:20:00 1100 E012-2
2015-02-01 07:00:00 2200 E012-1
2015-02-01 07:15:00 2200 E012-2
Now corresponding to Guid, we have different EventTypeId. Now when I see E012-2 to corresponding E012-1 for a GUID (this could be different, for example sake I gave like this) I need a difference in Timestamp in minutes.
Example: for first two rows, for GUID=1100 it's 30. For the 3rd and 4th rows it's 20 minutes. for Guid=1100 for 5th and 6th rows it's 15 mints for Guid=2200.
I am using MySQL.