2

hi i have a little bit confusion about this SQL Query in Android with SQlite

i have 3 simple tables table category

   |_id|name| 
   |1  |cat1
   |2  |cat2
   |3  |cat3
   |4  |cat4

table list

   |_id|name| 
   |1  |list1
   |2  |list2
   |3  |list3
   |4  |list4

and i have other table which contains the id s for previous tables

|_id|category|list   |name
| 1 |2       |1      |item1
| 2 |1       |2      |item2
| 3 |2       |3      |item3
| 4 |2       |1      |item4

i want to left join the cat table so that is always shown as cat1 (count 10) cat2 (count 2) cat3 (count 3) cat4 (count 4)

when i filter with where list = 1 then the category doesn show the null columns

any suggestions

4

1 回答 1

0

您可以使用子查询:

SELECT category.name as cat, B.catcount as catcount
FROM category LEFT JOIN (SELECT table3.category, count(*) AS catcount
FROM table3
WHERE table3.list=1
GROUP BY table3.category) as B ON category.[_id] = B.category;

应该给出如下输出:

|cat  |catcount|
|cat1 |    null|
|cat2 |       2|
|cat3 |    null|
|cat4 |    null|
于 2013-06-22T23:26:09.780 回答