SQL, between, SUBTIME select the date and time in the desired range
You can use the between operator to create a query between two dates and times.
SELECT * FROM sales WHERE date between '2020-03-06' AND '2020-03-07' SELECT * FROM sales WHERE date between '2020-03-06 10:30:08' AND '2020-03-06 10:45:08'
Function SUBTIME will help you create a date by subtracting dates.
SUBTIME(datetime, time_interval)
Subtract 2 days from 2020-03-06. The result will be the date 2020-03-04.
SUBTIME('2020-03-06', 2)
You can take 15 minutes away from 2020-03-06 10:45:08.
And get time 2020-03-06 10:30:08.
SUBTIME ('2020-03-06 10:45:08', '00:15:00')
You can use CURDATE() or NOW() to get the current date and time.
SELECT * FROM sales WHERE date between '2020-03-06' AND CURDATE() SELECT * FROM sales WHERE date between '2020-03-06 10:45:08' AND NOW()
This is a SQL query to get a sample of sales for the last two days.
SELECT * FROM sales WHERE date between SUBTIME (CURDATE(), 2) AND CURDATE()
This is a SQL query to get a sample of sales for the last 15 minutes.
SELECT * FROM sales WHERE date between SUBTIME (NOW(), '00:15:00') AND NOW()
And let’s sort the result in descending order of date.
SELECT * FROM sales WHERE date between SUBTIME (CURDATE(), 2) AND CURDATE() ORDER BY date DESC
Categories: