Home > other >  MySQL where query by hour diff of 2 timestamps
MySQL where query by hour diff of 2 timestamps

Time:08-03

I have a table with 2 timestamps: start_time and end_time. How can I query with conditions like select all where the diff of those 2 fields is more than X hours.

Also does the field type (timestamp vs datetime) has any impact on the query i'm trying to achieve?

CodePudding user response:

The data type of the fields does for sure have some differences.

As stated in MySQL Timestamp Difference, it is usually the case that, given two datetime fields, they get converted to timestamps in order to subtract them.

The query could be something like

SELECT *
FROM Table
WHERE TIMESTAMPDIFF(HOUR,start_time,end_time)>2

Edit: What written above is valid for MySQL, you can use DATEDIFF if you are working in SQL Server

  • Related