mysql - sql count by value stack by different value pt.2 -
i have database this:
table1(article) id | name | description | cat_id 0 | val1 | desc1 | 1 1 | val2 | desc2 | 2 2 | val3 | desc3 | 1 3 | val4 | desc4 | 2 4 | val5 | desc5 | 1 5 | val6 | desc6 | 2 table2(categories) id | name 0 | shoe 1 | glove 2 | hat
it should return:
articles_connected | categorie_name 3 | glove 0 | shoe 3 | hat
i thought solution:
select count(category.id) article_amount, category.name article_table article, category_table category category.id = article.cat_id group category.id
but code isn't correct. sql query doesn't count zero'd index count values larger 0. how change following query count 0'd index ?
select count(category.id) articles_connected, category.name arcticle article, categories category category.id = arcticle .categorie group category.id
if understand right, have no join shoes, want display shoes = 0 in result (so ignore last column of table1 in example).
if join table2, no match, shoes not in result query. so, in case work left outer join:
select count(article.id) articles_connected, category.name categories category left outer join arcticle article on category.id = arcticle .categorie group category.id
so count articles, regardless if in table1
Comments
Post a Comment