Add 2 hours to current time in MySQL?

Mysql

Mysql Problem Overview


Which is the valid syntax of this query in MySQL?

SELECT * FROM courses WHERE (now() + 2 hours) > start_time

note: start_time is a field of courses table

Mysql Solutions


Solution 1 - Mysql

SELECT * 
FROM courses 
WHERE DATE_ADD(NOW(), INTERVAL 2 HOUR) > start_time

See Date and Time Functions for other date/time manipulation.

Solution 2 - Mysql

You need DATE_SUB() OR DATE_ADD()

Solution 3 - Mysql

SELECT * FROM courses WHERE (NOW() + INTERVAL 2 HOUR) > start_time

Solution 4 - Mysql

The http://dev.mysql.com/doc/refman/4.1/en/date-and-time-functions.html#function_date-add">DATE_ADD()</a> function will do the trick. (You can also use the http://dev.mysql.com/doc/refman/4.1/en/date-and-time-functions.html#function_addtime">ADDTIME()</a> function if you're running at least v4.1.1.)

For your query, this would be:

SELECT * 
FROM courses 
WHERE DATE_ADD(now(), INTERVAL 2 HOUR) > start_time

Or,

SELECT * 
FROM courses 
WHERE ADDTIME(now(), '02:00:00') > start_time

Solution 5 - Mysql

This will also work

SELECT NAME 
FROM GEO_LOCATION
WHERE MODIFY_ON BETWEEN SYSDATE() - INTERVAL 2 HOUR AND SYSDATE()

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEi MaungView Question on Stackoverflow
Solution 1 - MysqlGlavićView Answer on Stackoverflow
Solution 2 - MysqlSergey GalashynView Answer on Stackoverflow
Solution 3 - MysqlDaniël van EedenView Answer on Stackoverflow
Solution 4 - Mysqllc.View Answer on Stackoverflow
Solution 5 - MysqlSoumyajit SwainView Answer on Stackoverflow