如何使这个sqlite查询工作?

时间:2012-06-06 21:41:43

标签: android mysql 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表中包含四个actor的所有电影。

我收到此运行时错误:SQLiteException : A group by clause is required before having

此查询在我的Mysqln终端中运行,但看起来sqlite不喜欢它。如何更改上述查询以在SQLite中获得相同的结果?

1 个答案:

答案 0 :(得分:0)

因此,您想要计算表中有四个条目的movie_id个数。 SQL查询应该是:

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