2013-10-17 17 views
1

我有一個運行sqlite 3.7.16.2的開發環境和一個運行sqlite 3.7.9的生產環境,我遇到了一些意想不到的向後兼容性問題。在sqlite中出現意外的向後不兼容性

我有一個表,看起來像這樣:

sqlite> select * from calls; 
ID|calldate|calltype 
1|2013-10-01|monthly 
1|2013-11-01|3 month 
1|2013-12-01|monthly 
2|2013-07-11|monthly 
2|2013-08-11|monthly 
2|2013-09-11|3 month 
2|2013-10-11|monthly 
2|2013-11-11|monthly 
3|2013-04-22|monthly 
3|2013-05-22|monthly 
3|2013-06-22|3 month 
3|2013-07-22|monthly 
4|2013-10-04|monthly 
4|2013-11-04|3 month 
4|2013-12-04|monthly 
5|2013-10-28|monthly 
5|2013-11-28|monthly 

使用SQLite的新版本(3.7.16.2)我可以用這個:

SELECT ID, MIN(calldate), calltype FROM calls WHERE calldate > date('NOW') GROUP BY ID;

這給了我:

ID|MIN(calldate)|calltype 
1|2013-11-01|3 month 
2|2013-11-11|monthly 
4|2013-11-04|3 month 
5|2013-10-28|monthly 

但是,當我在舊版本上運行相同的代碼o ˚F源碼(3.7.9),我得到這個:

ID|MIN(calldate)|calltype 
1|2013-11-01|monthly 
2|2013-11-11|monthly 
4|2013-11-04|monthly 
5|2013-10-28|monthly 

我通過改變here看去,卻無法弄清楚這是爲什麼仍然發生。有關如何解決此問題或如何重寫我的查詢的任何建議?

回答

0

您正在使用在SQLite 3.7.11中添加的擴展名。

在標準SQL中,不允許使用既不出現在GROUP BY子句中也不包含在聚合函數中的列。 (SQLite默默接受與MySQL的兼容性,但從組中的某個隨機記錄返回數據。)

要從記錄中獲取最小值的其他列,必須搜索每個組的最小值首先,然後加入這些與原表:

SELECT calls.ID, 
     calls.calldate, 
     calls.calltype 
FROM calls 
JOIN (SELECT ID, 
      MIN(calldate) AS calldate 
     FROM calls 
     WHERE calldate > date('now') 
     GROUP BY ID 
    ) AS earliest 
ON calls.ID  = earliest.ID  AND 
    calls.calldate = earliest.calldate