SQLite:计算问题的参与者

时间:2015-01-14 20:49:28

标签: android sqlite

我有3张桌子。我目前运行以下查询,但不起作用。

SELECT
  a._id,
  a.title,
  COUNT(DISTINCT user_id)
AS
  number_of_participants
FROM
  a, b, c
WHERE 
  a._id=b.fk AND
  b._id=c.fk

字段user_id仅存在于表c中。

解释我想要的内容:表a包含问题,而表b包含对问题的引用fk的答案。我想要计算已经为每个问题投票的用户数量。所以表c持有票。

修改 我的数据库方案:

Table a:
_id integer,
title text

Table b:
_id integer,
title text,
fk integer references a._id

Table c:
user_id text,
name text,
fk integer references b._id

1 个答案:

答案 0 :(得分:1)

自己找到解决方案:

我必须添加

group by a.title

修改

为了也支持没有参与者的问题,我不得不将其更改为双LEFT OUTER JOIN。