sqlite:使用复合选择查询其他列

时间:2011-02-04 20:58:12

标签: sql sqlite select

我有一个包含此列的表:

| 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);

有人知道我该怎么做吗?

由于

1 个答案:

答案 0 :(得分: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