给多个where子句一个ales

时间:2016-09-25 11:58:39

标签: sql sqlite

我喜欢这样做的事情

SELECT
  start.time  AS startTime,
  finish.time AS finishTime
FROM Train
WHERE _id = 3 AS start,
WHERE _id = 5 AS finish;

在SQL中是否有任何等价物,或者我必须创建两个不同的select语句。

3 个答案:

答案 0 :(得分:2)

假设_id是您的主键,您可以执行嵌套SELECT

SELECT 
    (SELECT start.time FROM Train Where _id=3) AS start
,   (SELECT finish.time FROM Train Where _id=5) AS finish

答案 1 :(得分:1)

另一种方法是使用条件聚合:

select min(case when _id = 3 then time end) as start_time,
       max(case when _id = 5 then time end) as end_time
from t
where _id in (3, 5);

答案 2 :(得分:0)

SELECT
 LAG(time, 2, 0) OVER () AS startTime
 time AS finishTime,
FROM Train
WHERE _id = 5
ORDER BY _id;

如果您能够计算开始和结束时间之间的偏移量,则可以使用LAG函数访问上一行的值。如果您不能,请提供完整的查询,或使用@Gordon Linoff解决方案。