2012-11-10 204 views
-1

如何將兩個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

可以使用派生表(子查詢),而不是創建一個實際的表:

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選擇允許它。這個查詢很可能不符合您的期望。