2010-08-14 47 views
4

我有一個是生產像這樣的查詢之間的差異獲取:如何2列

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

我想結果也包括EndTimestamp和StartTimestamp之間的區別:

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

如何在MySQL中執行此操作?

回答

6

如果表被命名,比如,t

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

當然,你不需要t.部分在選擇的列,如果未修飾的名稱StartTimestampEndTimestamp都在的情況下明確其餘的查詢。

+0

@Alex ...我想檢查'差異'是否大於'零'。我怎麼檢查這個? – someone 2014-06-08 07:38:31

+0

您可以使用'SELECT StartTimestamp,EndTimestamp,EndTimestamp - StartTimestamp AS Difference,case when(EndTimestamp - StartTimestamp)> 0 then'Greater than 0'else'Less than /等於0'結尾爲My_Flag FROM mytablename' – Karvy1 2015-11-25 18:57:58