Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 03-06-2004, 09:59 PM   #1 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
SQL Query Help Please

3 tables:

Articles ( ArticleID, ArticleTitle, ArticleText )
Category ( CatID, CatName )
ArtCat ( ArticleID, CatID )

the point here is to be able to be able to assign multiple categories for an article.

well, i want to make a list of categories with the number of articles in that category.

this query works, but it does not return the categories if there are no articles in them.
Code:
select Category.CatID,Category.CatName,count(ArtCat.ArticleID) 
from Category,ArtCat where Category.CatID=ArtCat.CatID group by Category.CatID
how can i return categories that don't have articles assigned to them?
__________________
Mike
sde is offline   Reply With Quote
Old 03-07-2004, 05:20 AM   #2 (permalink)
sdeming
Code Monkey
 
Join Date: Jul 2002
Location: Michigan
Posts: 85
sdeming is on a distinguished road
You want to use an OUTER join.

I'm pretty sure the syntax in MySQL goes like this:
Code:
SELECT Category.CatID,
       Category.CatName,
       count(ArtCat.ArticleID) 
  FROM Category,ArtCat 
  LEFT JOIN ArtCat ON Category.CatID = ArtCat.CatID 
 GROUP BY Category.CatID
I'm not 100% sure that's right, but it's something like that. Unfortunately OUTER JOIN syntax is different for different RDBMS' so I lose track since I don't work in MySQL much.
__________________
Scott
B4 09 BA 09 01 CD 21 CD 20 53 63 6F 74 74 24
sdeming is offline   Reply With Quote
Old 03-07-2004, 06:52 AM   #3 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
thanks Scott,

The problem is that ArtCat.CatID is not a unique field.

I'm working with MySQL and that is the error it complains about. That gets me a little closer though.

Thanks.
__________________
Mike
sde is offline   Reply With Quote
Old 03-07-2004, 06:59 AM   #4 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
Code:
FROM Category
the poblem was in that line. i took out the second table and it wors great! thanks scott!
__________________
Mike
sde is offline   Reply With Quote
Old 03-07-2004, 09:34 AM   #5 (permalink)
bdl
Senior Contributor
 
Join Date: May 2002
Location: vta.ca.usa
Posts: 555
bdl is on a distinguished road
If I may throw my own 2 cents into the hat; sdeming is absolutely right, you do need to perform a LEFT OUTER JOIN in order to retrieve a resultset regardless if there are categories with no articles. Reason being is, in a standard INNER JOIN, you're retrieving only the resultset that match all criteria, i.e. only categories with articles. If you change the query so you're performing a LEFT JOIN, it will retrieve all records from the table on the LEFT side of the equation (makes sense, right? ;-) ) and only those from the table on the right that match the criteria. So for example, breaking your SQL statement down further, let's look at it from the standpoint of LEFT JOIN.

Code:
SELECT Category.CatID,Category.CatName,count(ArtCat.ArticleID)
Ok so in the SELECT, the tables we want to use in the resultset are Category and ArtCat. Notice that Category is the table declaration on the left. It's that simple. If we wanted to retrieve all articles regardless if they have a category assigned to them, we would want all records from the table on the right, and it would become a RIGHT OUTER JOIN.

When you declare a LEFT or RIGHT OUTER JOIN, you skip the table declaration in the FROM (as you've already found out) and use it in the declaration for the LEFT JOIN itself. You then use the keyword ON to match the columns up. In a simple INNER JOIN, the ON would be something like using the WHERE clause. Furthermore, if we have two columns from different tables that have the same name, you can use the USING keyword instead, like
Code:
SELECT Category.CatID,
       Category.CatName,
       count(ArtCat.ArticleID) 
FROM Category
LEFT OUTER JOIN ArtCat
  USING (CatID) 
GROUP BY Category.CatID
Taking it one step further and cleaning the resultset up a bit, I'll use an ALIAS on the tables and on the COUNT so we have a little nicer formatting in the end, e.g.
Code:
SELECT c.CatID,
       c.CatName,
       COUNT(ac.ArticleID) AS ArticleCount
FROM Category AS c
LEFT OUTER JOIN ArtCat AS ac
  USING (CatID) 
GROUP BY c.CatID
So now, rather than having a result like COUNT(ArtCat.ArticleID) you can simply refer to it as ArticleCount (or whatever word strikes you as making sense). Strictly speaking, the OUTER keyword isn't needed, you can use simply LEFT or RIGHT join, but I find in circles where SQL is spoken most often they do use the full syntax in statements. I suppose it leaves very little chance for ambiguity in the statement.

Anyway, hope that clears things up a bit.
bdl is offline   Reply With Quote
Old 03-07-2004, 10:03 AM   #6 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
wow b, .. excellent post.

you think you could copy & paste that in a tutorial? i don't think i've read a 'join' explination so clear.

that really makes sense out of the differences between the 'joins'

thanks!
__________________
Mike
sde is offline   Reply With Quote
Old 03-07-2004, 10:48 AM   #7 (permalink)
bdl
Senior Contributor
 
Join Date: May 2002
Location: vta.ca.usa
Posts: 555
bdl is on a distinguished road
Quote:
Originally posted by sde
wow b, .. excellent post.

you think you could copy & paste that in a tutorial? i don't think i've read a 'join' explination so clear.

that really makes sense out of the differences between the 'joins'

thanks!
Hey no problem. I'll see if I can't come up with a little more complete explanation with an example for a tut.
bdl is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL Forum Belisarius Feedback 2 06-14-2004 06:16 AM
a query type of questiton sde Program Design and Methods 6 08-21-2003 04:55 PM
sql query help please sde PHP 7 05-05-2003 04:53 PM
sql question .. joining 3 tables sde PHP 8 03-03-2003 01:12 PM


All times are GMT -8. The time now is 08:15 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting