【问题标题】:MYSQL select query using count (*)MYSQL 使用 count (*) 选择查询
【发布时间】:2012-04-13 01:03:36
【问题描述】:

我有一个关于 MYSQL 中的选择查询的问题

我有两个不同的表,我想得到某个结果

我使用了 COUNT 方法,它只给了我结果 (>=1)

但实际上,我想使用所有计数为零,包括如何做到这一点?

我的查询是:

SELECT 
    first.subscriber_id, 
    second.tag_id,
    COUNT(*)
FROM 
    content_hits first  
    JOIN content_tag second ON first.content_id=second.content_id 
GROUP BY  
    second.Tag_id,first.Subscriber_id<br>

第一个表:Content_hits

CONTENT_ID  SUBSCRIBER_ID   
30          1   
10          10  
34          4   
32          2   
40          3 
28          3   
30          6   
31          8   
12          3 

第二个表:Content_tag

CONTENT_ID   TAG_ID
1            1
2            1
3            1
4            1
5            1
6            1
7            1
8            1
9            1
10           1
11           2
12           2
13           2
14           2

结果不完整例如:Subsrciber6 for tag_id=1 应该有一个 count(*)=0

subscriber_id   tag_id   COUNT(*)
1               1        4
2               1        7
3               1        2
4               1        1
5               1        3
7               1        2
8               1        1
9               1        1
10              1        3
1               2        2
2               2        3
3               2        2

【问题讨论】:

  • 如果您肯定会在 content_hits 中有记录,但不一定在 content_tag 中,那么将联接更改为左联接,无论 content_tag 中是否有条目,都将返回 content_hits 中的所有条目

标签: mysql sql count select-query


【解决方案1】:

现在您已经进一步详细说明了您真正想要实现的目标,可以看出问题要复杂得多。您实际上想要subscriber_idtag_id 的所有组合,然后计算联接表产品中的实际条目数。哇。所以这里是 SQL:

SELECT combinations.tag_id,
       combinations.subscriber_id,

-- correlated subquery to count the actual hits by tag/subscriber when joining
-- the two tables using content_id

       (SELECT count(*)
        FROM content_hits AS h
        JOIN content_tag AS t ON h.content_id = t.content_id
        WHERE h.subscriber_id = combinations.subscriber_id
        AND t.tag_id = combinations.tag_id) as cnt

-- Create all combinations of tag/subscribers first, before counting anything
-- This will be necessary to have "zero-counts" for any combination of
-- tag/subscriber

FROM (
  SELECT DISTINCT tag_id, subscriber_id
  FROM content_tag
  CROSS JOIN content_hits
) AS combinations

【讨论】:

  • 实际上,content_id 是属于 tag_id 的内容的 id。但是假设每个订阅者没有属于该标签的 content_id,则每个订阅者对于某个标签的计数可能为零。
  • @holy:那么我想我正确理解了您查询的意图。试试我的建议...
  • 不工作:(我试图拆分查询的两个成员:SELECT first.subscriber_id, second.tag_id, COUNT(*) FROM content_hits first LEFT OUTER JOIN content_tag second ON first.content_id = second.content_id GROUP BY second.Tag_id, first.Subscriber_id. 但结果是:1 1 4 2 1 7 3 1 2 4 1 1 5 1 3 7 1 2 8 1 1 9 1 1 10 1 3 Subsriber6 应该在结果:6 1 0 (6:nb of sub 1:tag_id 0:count(*))
  • 您要统计每个订阅者的标签数量,还是每个标签的订阅者数量?或者你想计算每个 content_id 的订阅者和标签的数量?因为后者是一个完全不同的查询......!
  • 我想统计每个订阅者的 content_id 数量并按标签排序(因为许多 content_id 属于一个标签)
【解决方案2】:

不确定,但这是你想要的吗?

 SELECT first.subscriber_id, second.tag_id, COUNT(*) AS c 
 FROM content_hits first JOIN content_tag second ON first.content_id=second.content_id 
 GROUP BY second.Tag_id,first.Subscriber_id HAVING c = 0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多