2012-06-06 54 views
1

SQLite中我無法運行此查詢:我如何使這個SQLite查詢工作?

Cursor c = mDb.rawQuery(
       "SELECT count(sim1.movie_id) FROM stars_in_movies sim1 WHERE " 
         + "EXISTS (SELECT 1 FROM stars_in_movies sim2 WHERE " 
         + "sim1.movie_id = sim2.movie_id HAVING COUNT(*) = 4) " 
         , null); 

此查詢獲取所有的stars_in_movies表中有四個演員在他們的電影。

我得到這個運行時錯誤:SQLiteException : A group by clause is required before having

該查詢運行在我Mysqln終端,但它看起來像SQLite不喜歡它。如何更改上述查詢以在SQLite中獲得相同的結果?

回答

0

所以,你想要計算你的表中有四個條目的movie_id的數量。 SQL查詢應該是:

SELECT COUNT(*) 
FROM 
(
    SELECT 1 
    FROM stars_in_movies 
    GROUP BY movie_id 
    HAVING COUNT(*) = 4 
) s_i_m 
+0

所有SQL查詢不一定與SQLite一起使用... – GAMA