将 () 算作某物

发布于 2024-11-24 19:30:28 字数 1127 浏览 1 评论 0原文

我有下表: 类别:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰葑 2024-12-01 19:30:28
SELECT c1.cat_id, c1.cat_cat, c1.cat_name,
(
  SELECT COUNT(*)
  FROM categories c2
  WHERE c2.cat_cat=c1.cat_id
) AS cat_subcats
FROM categories c1
WHERE c1.cat_cat=0

它是一个标准的SELECT,除了计算当前类别的子类别的子选择之外。

SELECT c1.cat_id, c1.cat_cat, c1.cat_name,
(
  SELECT COUNT(*)
  FROM categories c2
  WHERE c2.cat_cat=c1.cat_id
) AS cat_subcats
FROM categories c1
WHERE c1.cat_cat=0

Its a standard SELECT, except the subselect that counts the subcategories for the current category.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文