将 () 算作某物
我有下表: 类别:
cat_id | cat_cat | cat_name
1 | 0 | name1
2 | 0 | name2
3 | 1 | name3
4 | 2 | name4
5 | 2 | name5
6 | 0 | name6
7 | 1 | name7
我尝试选择 cat_cat=0 的所有类别,并选择以 cat_cat 作为当前 cat_id 的每条记录的计数,基本上给出以下结果:
cat_id | cat_cat | cat_name | cat_subcats
1 | 0 | name1 | 2
2 | 0 | name2 | 2
6 | 0 | name7 | 0
我尝试了多个查询,当前的查询如下:
SELECT i.* , COUNT( i2.icon_id ) AS subcats
FROM themes_icons i, themes_icons i2
WHERE i.icon_cat = '0'
AND i2.icon_cat = i.icon_id
GROUP BY i.icon_cat, i2.icon_id
ORDER BY i.icon_order ASC
该查询似乎给了我以下结果:
cat_id | cat_cat | cat_name | cat_subcats
1 | 0 | name1 | 1
1 | 0 | name1 | 1
2 | 0 | name2 | 1
2 | 0 | name2 | 1
6 | 0 | name7 | 0
任何人都知道我如何获得所需的结果。我也尝试过 join 语句,但它们总是给我返回错误。
提前致谢。
I have the following table:
categories:
cat_id | cat_cat | cat_name
1 | 0 | name1
2 | 0 | name2
3 | 1 | name3
4 | 2 | name4
5 | 2 | name5
6 | 0 | name6
7 | 1 | name7
I am trying to select all the categories with cat_cat=0, and selecting a count of every record that has cat_cat as the current cat_id, essentially giving me the following result:
cat_id | cat_cat | cat_name | cat_subcats
1 | 0 | name1 | 2
2 | 0 | name2 | 2
6 | 0 | name7 | 0
I have tried multiple queries and my current one is as follows:
SELECT i.* , COUNT( i2.icon_id ) AS subcats
FROM themes_icons i, themes_icons i2
WHERE i.icon_cat = '0'
AND i2.icon_cat = i.icon_id
GROUP BY i.icon_cat, i2.icon_id
ORDER BY i.icon_order ASC
This query seems to give me the following results:
cat_id | cat_cat | cat_name | cat_subcats
1 | 0 | name1 | 1
1 | 0 | name1 | 1
2 | 0 | name2 | 1
2 | 0 | name2 | 1
6 | 0 | name7 | 0
Anyone know how I can get the desired results. I've tried join statements too, but they keep giving me back errors.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它是一个标准的
SELECT
,除了计算当前类别的子类别的子选择之外。Its a standard
SELECT
, except the subselect that counts the subcategories for the current category.