本文介绍了sql按值堆栈按不同的值计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库如下:

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
6 | val7 |   desc7     |   0


table2(categories)
id | name
0  | shoe
1  | glove
2  | hat

它应该返回:

articles_connected | categorie_name
        3          |  glove
        0          |  shoe
        3          |  hat

我尝试过:

SELECT
    COUNT(Category.id) as article_amount, Category.name
FROM
    article_table as Article,
    category_table as Category
WHERE
    Category.id =  Article.cat_id

此代码仅将全部视为一个,而不会拆分我的类别.我不确定出什么问题.

this code just counts all as one, doesn't split up my categories. I'm not sure what goes wrong.

推荐答案

尝试

SELECT
COUNT(Category.id) as article_amount, Category.name
FROM
article_table as Article,
category_table as Category
WHERE
     Category.id =  Article.cat_id
group by Category.name

这篇关于sql按值堆栈按不同的值计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 06:21
查看更多