SQL连接问题

发布于 2024-11-25 23:43:33 字数 401 浏览 2 评论 0原文

我有两个这样的表:

category_table:

data_id  cat_id
      1       1
      1       2
      3       1
      3       3
      4       5
      4       6

data_table:

data_id  example_data
      1             x
      2             y
      3             m
      4             i

我需要的是计算与类别 1 AND 2 分组的 data_table 记录的数量

I have 2 tables like this :

category_table:

data_id  cat_id
      1       1
      1       2
      3       1
      3       3
      4       5
      4       6

data_table:

data_id  example_data
      1             x
      2             y
      3             m
      4             i

what I need is to count number of data_table records that are grouped with category 1 AND 2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

苍暮颜 2024-12-02 23:43:33

编辑:重新阅读您的问题后,这是您要寻找的吗? data_table 中的元素计数,其中category_table 中存在类别 1 和 2 的条目?

select
    count(*)
from
    data_table d
    inner join category_table c1
        on  d.data_id = c1.data_id
    inner join category_table c2
        on  d.data_id = c2.data_id
where
    c1.cat_id = 1
    c2.cat_id = 2

(下面的旧答案...)

如果您想要对所有记录进行直接计数:

select
    count(*)
from
    data_table d
    inner join category_table c
        on  d.data_id = c.data_id
where
    c.cat_id in (1, 2)

如果您想要每个类别的计数:

select
    c.cat_id, count(*)
from
    data_table d
    inner join category_table c
        on  d.data_id = c.data_id
where
    c.cat_id in (1, 2)
group by
    c.cat_id

Edit: After re-reading your question, is this what you're looking for? A count of elements from data_table where there exists an entry in the category_table for both categories 1 and 2?

select
    count(*)
from
    data_table d
    inner join category_table c1
        on  d.data_id = c1.data_id
    inner join category_table c2
        on  d.data_id = c2.data_id
where
    c1.cat_id = 1
    c2.cat_id = 2

(old answer below this...)

If you want a straight count of all records:

select
    count(*)
from
    data_table d
    inner join category_table c
        on  d.data_id = c.data_id
where
    c.cat_id in (1, 2)

If you want a count for each category:

select
    c.cat_id, count(*)
from
    data_table d
    inner join category_table c
        on  d.data_id = c.data_id
where
    c.cat_id in (1, 2)
group by
    c.cat_id
猫弦 2024-12-02 23:43:33

如果您想计算 data_table 中 data_id 与 cat_id 1 以及 cat_id 2 相关联的行数...

select count(*) from data_table
where data_id in 
 (select data_id from category_table as ct1 where ct1.cat_id=1 and exists
  (select * from category_table as ct2 where ct2.cat_id=1 and ct2.data_id=ct1.data_id)
  )

在您的情况下,这将返回对于 data_table (1 : x) 的第一行,值为 1,因为 data_id (1) 是category_table 中唯一具有 cat_id=1 的行以及猫_id=2。

If you want to count the rows in data_table whose data_id is associated (in category_table) with a cat_id of 1 and also with a cat_id of 2...

select count(*) from data_table
where data_id in 
 (select data_id from category_table as ct1 where ct1.cat_id=1 and exists
  (select * from category_table as ct2 where ct2.cat_id=1 and ct2.data_id=ct1.data_id)
  )

In your case, this will return a value of 1, for the first row of the data_table (1 : x), since that data_id (1) is the only one with a row in category_table for which cat_id=1 and also a row for which cat_id=2.

铃予 2024-12-02 23:43:33
SELECT COUNT(CAT_ID) 
FROM CATEGORY_TABLE 
JOIN DATA_TABLE ON CATEGORY_TABLE.CAT_ID=DATA_TABLE.DATA_ID 
WHERE CATEGORY_TABLE.CAT_ID IN (1,2) 
SELECT COUNT(CAT_ID) 
FROM CATEGORY_TABLE 
JOIN DATA_TABLE ON CATEGORY_TABLE.CAT_ID=DATA_TABLE.DATA_ID 
WHERE CATEGORY_TABLE.CAT_ID IN (1,2) 
过潦 2024-12-02 23:43:33
 select Cat_id,COUNT(*) [number of data_table] from data_table
 inner join category_table on category_table.data_id = data_table.data_id 
 group by Cat_id

或者

  select Cat_id,COUNT(*) [number of data_table] from category_table
  group by Cat_id
 select Cat_id,COUNT(*) [number of data_table] from data_table
 inner join category_table on category_table.data_id = data_table.data_id 
 group by Cat_id

OR

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