If i have two rows
id created start stop
1 28-01-2011 23:00 28-01-2011 23:00 28-01-2011 23:01
2 28-01-2011 23:10 28-01-2011 23:10 28-01-2011 23:11
What query can i run to get the total elapsed time for one date, so for 28-01-2011 the total time was 2 minutes.
Is this possible?
well one option is straight forward:
select sum(unix_timestamp(stop) -
unix_timestamp(start))/60 from table
where date(created) = '28-01-2011';
mysql> select now(), date_add(now(), interval 10 minute);
+---------------------+-------------------------------------+
| now() | date_add(now(), interval 10 minute) |
+---------------------+-------------------------------------+
| 2011-10-04 13:29:56 | 2011-10-04 13:39:56 |
+---------------------+-------------------------------------+
1 row in set (0.01 sec)
mysql> select sum(unix_timestamp(now()) -
unix_timestamp(date_add(now(), interval 10 minute)));
+----------------------------------------------------------------------------------+
| sum(unix_timestamp(now()) - unix_timestamp(date_add(now(), interval 10 minute))) |
+----------------------------------------------------------------------------------+
| -600 |
+----------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> select sum(unix_timestamp(now()) -
unix_timestamp(date_add(now(), interval 10 minute))) / 60 ;
+---------------------------------------------------------------------------------------+
| sum(unix_timestamp(now()) - unix_timestamp(date_add(now(), interval 10 minute))) / 60 |
+---------------------------------------------------------------------------------------+
| -10.0000 |
+---------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
Related
I've been trying to solve this problem for a few hours , so far what i have reached is something like this:
SELECT * FROM mytable WHERE DATE(datetime_column)
BETWEEN DATE_SUB(DATE_FORMAT(NOW(),'%Y-%M-01'),INTERVAL 1 DAY) AND NOW()
AND HOUR(datetime_column) > = '13'
ORDER BY
datetime_column
Besides the day interval, i also need the hour interval which must be between 13h pm from last day of the last month till midday for present date.
The fiddle doesnt seem to work with the current query but its working in my database, but check fiddle for more info:
https://www.db-fiddle.com/f/a3jui7aPMevU81Cka4BqXg/0
And sample data:
CREATE TABLE mytable (
id INT,
datetime_column DATETIME
);
INSERT INTO mytable (id,datetime_column) VALUES
(1,'2020-03-31 08:00:00'),
(2,'2020-03-31 13:00:00'),
(3,'2020-04-02 14:30:00'),
(4,'2020-04-06 18:00:00'),
(5,'2020-04-21 05:00:00'),
(6,'2020-04-23 13:00:00'),
(7,'2020-04-23 14:00:00');
If more info is necessary please let me know.
I think this what you want
SELECT * FROM mytable
WHERE (
DATE(datetime_column) > LAST_DAY(CURDATE() - INTERVAL 1 MONTH)
AND datetime_column < (CURDATE() + INTERVAL 12 HOUR)
)
OR (
DATE(datetime_column) = LAST_DAY(CURDATE() - INTERVAL 1 MONTH)
AND HOUR(datetime_column) >= 13
);
+------+---------------------+
| id | datetime_column |
+------+---------------------+
| 2 | 2020-03-31 13:00:00 |
| 3 | 2020-04-02 14:30:00 |
| 4 | 2020-04-06 18:00:00 |
| 5 | 2020-04-21 05:00:00 |
+------+---------------------+
2 rows in set (0.00 sec)
Here LAST_DAY(CURDATE() - INTERVAL 1 MONTH) gives the last day of previous month.
And datetime_column < (CURDATE() + INTERVAL 12 HOUR) tests if that time is before mid day of today.
I want a MySQL query that will fetch all Fridays with date for the year 2017.
I know that SQL query for the same is:
SELECT Fridays = DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), n.num)
FROM (SELECT TOP 366 num = ROW_NUMBER() OVER(ORDER BY a.NAME)-1 FROM dbo.syscolumns a, dbo.syscolumns b) n
WHERE DATENAME(weekday, DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), n.num)) = 'Friday'
I am looking for a MySQL alternative for the same.
This is an exact conversion of your sql logic in mysql
SELECT DATE_ADD(MAKEDATE(year(now()),1), INTERVAL #num:=#num+1 DAY)
From (select #num:=-1) num
where DAYNAME(DATE_ADD(MAKEDATE(year(now()),1), INTERVAL #num:=#num+1 DAY))="Friday"
limit 365
Mysql does not have row_number function until version 8. Prior to version 8 row_number can be simulated using variables.
select s.num , date_add(str_to_date(concat('2016','/','12','/','31'),'%Y/%m/%d'), interval s.num day) dt,
dayname(date_add(str_to_date(concat('2016','/','12','/','31'),'%Y/%m/%d'), interval s.num day)) dy
from
(
select #num:=#num + 1 num from information_schema.columns,(select #num:=0) n
limit 35
) s
where dayname(date_add(str_to_date(concat('2016','/','12','/','31'),'%Y/%m/%d'), interval s.num day)) = 'Friday';
information_schema.columns is similar to dbo.syscolumns.
------+------------+--------+
| num | dt | dy |
+------+------------+--------+
| 6 | 2017-01-06 | Friday |
| 13 | 2017-01-13 | Friday |
| 20 | 2017-01-20 | Friday |
| 27 | 2017-01-27 | Friday |
| 34 | 2017-02-03 | Friday |
+------+------------+--------+
5 rows in set, 2 warnings (0.26 sec)
SELECT * FROM table WHERE ([date] BETWEEN date_start and date_end) AND
DAYNAME([date])='Friday'
Source of the query: Get data for every friday between two dates
I have these rows
id | start_time |
1 | 2018-06-15 02:00:00 |
2 | 2018-06-15 02:45:00 |
3 | 2018-06-15 03:45:00 |
I want to select rows that are 1 hour before the start_time. So if the time is 2018-06-15 01:00:00 then the first row should be returned.
How do i do this? I've tried below but i don't know how to subtract 1 hour from start_time.
SELECT *
FROM table1
WHERE DATE_FORMAT(start_time, '%Y-%m-%d %H') <= DATE_FORMAT(NOW(), '%Y-%m-%d %H');
To subtract hours ,use date_sub function
In your case
SELECT DATE_SUB(DATE_FORMAT(start_time, '%Y-%m-%d %H'), INTERVAL 1 HOUR)
How I will construct query that will allow to count number of entries from the database which date is in the interval of 20 days, I mean current date minus 20 days to get exact 20 days, and return 0 if no record was found?
For this, you just want to refer to the Date and Time function help:
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
> select NOW();
+---------------------+
| NOW() |
+---------------------+
| 2014-05-13 14:41:20 |
+---------------------+
> select DATE_ADD(NOW(), INTERVAL -20 DAY);
+-----------------------------------+
| DATE_ADD(NOW(), INTERVAL -20 DAY) |
+-----------------------------------+
| 2014-04-23 14:41:13 |
+-----------------------------------+
You are probably looking for something like:
SELECT COUNT(*) FROM tablename WHERE datefield BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL -20 DAY);
Is there a difference between:
SELECT DATE_ADD('2005-01-01', INTERVAL 3 MONTH);
and
SELECT '2005-01-01' + INTERVAL 3 MONTH;
No, they're the same.
I asked a similar question just now and found the answer myself. Here's the justification why they are the same:
SELECT BENCHMARK(20000000, DATE_ADD(NOW(), INTERVAL 3 MONTH));
+--------------------------------------------------------+
| BENCHMARK(20000000, DATE_ADD(NOW(), INTERVAL 3 MONTH)) |
+--------------------------------------------------------+
| 0 |
+--------------------------------------------------------+
1 row in set (1.70 sec)
SELECT BENCHMARK(20000000, NOW() + INTERVAL 3 MONTH);
+-----------------------------------------------+
| BENCHMARK(20000000, NOW() + INTERVAL 3 MONTH) |
+-----------------------------------------------+
| 0 |
+-----------------------------------------------+
1 row in set (1.71 sec)