结合两个sqlite语句

时间:2012-11-10 07:21:22

标签: sqlite

如何将两个sqlite语句组合成一个?我只想要一个语句来获得结果。

create table lowprice as select  code,date,min(close) as min_close from quote where date between '20120730' and '20121030' group by code;
select  * from lowprice where lowprice.date between  '20121025'  and  '20121030' ;

1 个答案:

答案 0 :(得分:1)

您可以使用派生表(子查询)而不是创建实际表:

SELECT code, date, min_close
FROM
(
    SELECT code, date, min(close) as min_close
    from quote
    WHERE date BETWEEN '20120730' AND '20121030'
    GROUP BY code
) AS lowprice
WHERE lowprice.date BETWEEN '20121025' AND '20121030' ;

但是,此查询看起来很可疑,因为您按code进行分组,但选择date。这是一个名为GROUP BY and HAVING with hidden columns的MySQL扩展。如果你理解为什么这个查询在大多数数据库中无效以及MySQL选择允许它的原因,你应该只使用这个扩展。这个查询很可能没有达到预期效果。