2011-02-04 42 views
1

我有一個包含一個表此列:源碼:查詢與複合附加列選擇

| name | city | time | version | 

我使用這個查詢來找到新不會忽略所有行兩個版本之間:

select name, city from adresses where version = 2 
except 
select name, city from adresses where version = 1 

它的工作但我想檢索時間值,但每個版本之間的時間值更改。

,我想這樣的事情:

NOT WORKING 
select time from adresses where version = 2 and 
(select name, city from adresses where version = 1 
except select name, city from adresses where version = 2); 

有人知道我能做到這一點?

感謝

回答

2

不知道我在正確理解你的問題,但根據你寫的查詢,我覺得這就是你其實想:

SELECT time FROM adresses a 
INNER JOIN (
    select name, city from adresses where version = 1 
    except 
    select name, city from adresses where version = 2 
) new ON new.name = a.name AND new.city = a.city 
WHERE version = 2 

如果你正在尋找的時間兩個版本的記錄(這是你也提到你的問題)之間的區別,那麼你可以做這樣的事情:

SELECT a2.*, a2.time - a1.time as time_diff 
FROM adresses a1 
INNER JOIN adresses a2 ON a1.name = a2.name AND a1.city = a2.city 
WHERE a1.version = 1 AND a2.version = 2 
+0

嗨。非常感謝。這是我想要的第一個查詢:-) –