从3个表中选择SQLite

时间:2016-12-22 00:45:13

标签: sqlite

我有类似的表结构,如下所示:

表1 - 类别c

cat_id | name
1        Music
2        Books

表2 - 未知你

unk_id | eng_id_fk | progress 
1          1          0
2          2          0
3          3          1
4          4          0
5          5          1
6          6          0
7          7          1

表3 - eng e

e_id | e_name   | category_id_fk
1      example1       1
2      example2       1
3      example3       1
4      example4       2
5      example5       2
6      example6       1
7      example7       1

我想要带有输出的select语句:

c.cat_id | c.name | count(u.unk_id) where u.eng_id_fk = e.e_id | count(u.unk_id where progress>0)
1.         Music        5                                              2     
2.         Books        2                                              1

我试过这样做,我有类似的东西:

 SELECT c.cat_id,c.name,count(u.unk_id) AS counts FROM category c, eng e, unknown u  WHERE e.e_id= u.ENG_ID_FK  AND e.category_id_fk=c.cat_id group by c.name order by c.cat_id;

所以我几乎拥有一切。我只是不知道我如何计算行数unk_id进度> 0。

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。我只需要知道如何在count()中指定条件以获取unknown_id的计数,其中progress> 0。

select c.cat_id, c.name, count(u.unk_id) AS COUNTS,
count(case when u.progress>0 then 1 else null end) AS PROGRESS
FROM category c, eng e, unknown u 
WHERE e.e_id=u.eng_id_fk AND e.category_id_fk = c.cat_id
group by c.name order by c.cat_id;