Frederic Close
Frederic Close

Reputation: 9639

subtract a datetime from a timestamp with mysql

I have to do a subtraction between two fields from two different tables, one of the field is a Datetime the other field is a Timestamp.

What's the best way to achieve that ?

convert first to unix timestamp ?

something like:

select UNIX_TIMESTAMP(t1.col1) - UNIX_TIMESTAMP(t2.col2) from t1, t2 ...

Upvotes: 0

Views: 1060

Answers (1)

Andreas Wederbrand
Andreas Wederbrand

Reputation: 39951

TIMESTAMPDIFF is by far the most versatile alternative.

select timestampdiff(SECOND, t1.col1, t2.col2);

See it in fiddle

Upvotes: 2

Related Questions