3

i have a problem concerning a select query in MYSQL

i have two different tables and i want to obtain a certain result

i used COUNT method which gave me only the results (>=1)

But in reality , i want to use all counts with zero included how to do it?

My query is:

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>

First table:Content_hits

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

Second table: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

Result but incomplete For example:Subsrciber6 for tag_id=1 should have a 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
1
  • If you definitely will have a record in content_hits but not necessarily in content_tag then change the join to a left join which will return all entries in content_hits whether or not there's an entry in content_tag Commented Mar 29, 2012 at 11:34

2 Answers 2

2

Now that you have further elaborated on what you actually want to achieve, it can be seen that the problem is much more complex. You actually want all combinations of subscriber_id and tag_id, and then count the number of actual entries in the joined table product. whew. So here goes the 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
Sign up to request clarification or add additional context in comments.

7 Comments

in fact,the content_id is the id of a content belonging to a tag_id.But each subscriber could have for a certain tag a zero count assuming that he doesn't have a content_id belonging to this tag.
@holy: Then I think I correctly understood the intent of your query. Try my suggestion...
not working :( i have tried to split the two members of the query : 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. But the result is: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 should figure in the result: 6 1 0 (6:nb of sub 1:tag_id 0:count(*))
Do you want to count the number of tags per subscriber, or the number of subscribers per tag? Or do you want to count the number of subscribers and tags per content_id? Because the latter is an entirely different query...!
i want to count the number of content_id for each subscriber and order them by tag (because many content_id belong to one tag)
|
0

Not sure, but is this what you want?

 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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.