MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMySQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 6th, 2013, 03:51 AM
doush. doush. is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Posts: 119 doush. User rank is Private First Class (20 - 50 Reputation Level)doush. User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Day 5 h 57 m 14 sec
Reputation Power: 8
JOIN count wont show items with 0 results

Hey all,

I've been playing around with this query to get a count of then number of times a particular tag occurs in a relational table.

I have 2 tables

TAGS
id, tag
1, tag1
2, tag2
3, tag3
4, tag4

COMMENT_TAGS
comment_id, tag_id
1, 1
2, 1
3, 2

Here is the I've come up with

SELECT * FROM (SELECT T.tag, COUNT(C.tag_id) AS num FROM tags T JOIN comment_tags C ON T.id = C.tag_id GROUP BY T.tag) AS A WHERE A.tag LIKE '%tag%' LIMIT 10

I get the following

tag, num
tag1, 2
tag2, 1

What I'm trying to get it

tag, num
tag1, 2
tag2, 1
tag3, 0
tag4, 0

Am I trying to do this the wrong way?

Any help would be appreciated.

Reply With Quote
  #2  
Old February 6th, 2013, 04:22 AM
requinix's Avatar
requinix requinix is online now
Still alive
Click here for more information.
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,698 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 4 Days 4 h 53 m
Reputation Power: 8969
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to requinix
Quote:
JOIN count wont show items with 0 results

That is exactly correct. A JOIN across two tables requires records in both tables; if one table does not have matching rows (according to whatever condition you used) then the corresponding rows from the original table will not appear either. Which sounds right in my head.

You can use outer joins: LEFT [OUTER] and RIGHT [OUTER] JOIN. Values from rows that don't exist will be NULL. COUNT will then not count those rows.
Without double-checking my quick copy/paste,
Code:
SELECT T.tag, COUNT(C.tag_id) AS num FROM tags T LEFT JOIN comment_tags C ON T.id = C.tag_id GROUP BY T.tag

Besides the added "LEFT" it's the same query as before.
Comments on this post
doush. agrees!

Reply With Quote
  #3  
Old February 6th, 2013, 05:15 AM
doush. doush. is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Posts: 119 doush. User rank is Private First Class (20 - 50 Reputation Level)doush. User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Day 5 h 57 m 14 sec
Reputation Power: 8
Quote:
Originally Posted by requinix
That is exactly correct. A JOIN across two tables requires records in both tables; if one table does not have matching rows (according to whatever condition you used) then the corresponding rows from the original table will not appear either. Which sounds right in my head.

You can use outer joins: LEFT [OUTER] and RIGHT [OUTER] JOIN. Values from rows that don't exist will be NULL. COUNT will then not count those rows.
Without double-checking my quick copy/paste,
Code:
SELECT T.tag, COUNT(C.tag_id) AS num FROM tags T LEFT JOIN comment_tags C ON T.id = C.tag_id GROUP BY T.tag

Besides the added "LEFT" it's the same query as before.


That worked perfect! Thanks heaps for that. Makes sense now.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > JOIN count wont show items with 0 results

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap