Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get difference between 2 columns

I have a query that is producing something like this:

StartTimestamp  |  EndTimestamp
================================
100             | 450
--------------------------------
150             | 500

I'd like the result to also include the difference between EndTimestamp and StartTimestamp:

StartTimestamp  |  EndTimestamp  |  Difference
==============================================
100             | 450            | 350
----------------------------------------------
150             | 600            | 450

How do I do this in MySQL?

like image 994
StackOverflowNewbie Avatar asked Aug 14 '10 04:08

StackOverflowNewbie


1 Answers

If the table is named, say, t:

SELECT t.StartTimestamp, t.EndTimestamp, t.EndTimestamp - t.StartTimestamp AS Difference
FROM   &c

Of course, you don't need the t. parts in the select's columns if the undecorated names StartTimestamp and EndTimestamp are unambiguous in the context of the rest of your query.

like image 170
Alex Martelli Avatar answered Oct 12 '22 09:10

Alex Martelli