Home > database >  How can I write this SQL query for date and time?
How can I write this SQL query for date and time?

Time:11-01

I'm trying to make a query that will look like SELECT * from `table` WHERE args I need to have my args the date from 'yesterday' and time 23:00 until date'Today' and time 22:59.

I'm very bad at writing SQL and I will need masters help for this one. I know how to write basic SQL and I'm not sure how to write. Date and time are 2 different rows.

Basic SQL stuff. Expecting all data from date yesterday starting with time 23:00 until date = today and time = 22:59 Logically is simple, I'm just bad at writing SQL logic. Date has to be always generated based on current date, time will be static.

SELECT * from `table` WHERE date= Yesterday And time= 23:00 TO date=today time=22:59

Edit: Database is MariaDB from Xamp

CodePudding user response:

You may try the below query:

SELECT * FROM `table` WHERE date BETWEEN CONCAT(date_sub(curdate(),interval 1 day),' 23:00') AND CONCAT(curdate(),' 22:59');

Please double check your database timezone.

CodePudding user response:

Assuming this is MySQL (MariaDB) and there are in fact two columns called date and time this could be written as

SELECT * FROM `table` WHERE
(date = SUBDATE(CURDATE(), 1) AND time >= '23:00') OR (date = CURDATE() AND time < '23:00');

This is a bit unusual though. Usually you would have as single column of type datetime or timestamp instead of two columns, and they should not be called date or time because these are keywords.

  • Related