If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Double left join from same table issue

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-19-06, 13:59
monkey-ninja monkey-ninja is offline
Registered User
 
Join Date: Mar 2006
Posts: 1
Question Double left join from same table issue

Hi I am looking for some help with a MySQL problem that I have had for some time now.

I have two tables, one (articles) containing the columns id and title, and the other (comments) containing the columns id, name, status, and parent. I want to join the comments table onto the articles table twice so that I can have a column containing the total number of active comments (status=1) for each article, and one containing the total number of inactive comments (status=0). I have had little success in getting the columns to form correctly. I am attempting this problem with the following query:-

SELECT articles.title, COUNT(comments1.id) AS active, COUNT(comments2.id) AS inactive
FROM articles
LEFT JOIN comments AS comments1 ON (articles.id=comments1.parent AND comments1.status=1)
LEFT JOIN comments AS comments2 ON (articles.id=comments2.parent AND comments2.status=0)
GROUP BY articles.id;

Unfortunately active and inactive produce the same, wrong number per article.

BTW I am restricted to using MySQL 4.0 so cannot get round this problem using subqueries in the SELECT.

If anyone can help I will be very grateful.

Monkey Ninja
Reply With Quote
  #2 (permalink)  
Old 03-19-06, 20:44
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Code:
SELECT articles.title
     , sum(case when comments.status=1
                then 1 else 0 end) AS active
     , sum(case when comments.status=0
                then 1 else 0 end) AS inactive
  FROM articles
LEFT 
  JOIN comments AS 
    ON articles.id = comments.parent 
GROUP 
    BY articles.title
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 03-21-06, 08:41
gtk gtk is offline
Registered User
 
Join Date: Oct 2004
Location: Paris, FRANCE
Posts: 132
Quote:
Originally Posted by r937
Code:
SELECT articles.title
     , sum(case when comments.status=1
                then 1 else 0 end) AS active
     , sum(case when comments.status=0
                then 1 else 0 end) AS inactive
  FROM articles
LEFT 
  JOIN comments AS 
    ON articles.id = comments.parent 
GROUP 
    BY articles.title
This maybe simpler

Code:
SELECT
  articles.title,
  SUM(IF(comments.status = 1, 1, 0) AS "active",
  SUM(IF(comments.status = 0, 1, 0) AS "inactive",
FROM articles
  LEFT JOIN comments AS
    ON (articles.id = comments.parent)
GROUP 
  BY articles.title
isn't it ?
Reply With Quote
  #4 (permalink)  
Old 03-21-06, 11:24
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
no, it's not simpler

you have substituted a non-standard function (IF) for my standard sql CASE expression

you have needlessly double-quoted the column aliases

you have needlessly parenthesized the join condition

unfortunately, you copied over my syntax error (keyword AS without table alias name), but you also introduced a new syntax error, the dangling comma

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 03-21-06, 12:19
gtk gtk is offline
Registered User
 
Join Date: Oct 2004
Location: Paris, FRANCE
Posts: 132
Quote:
... you have substituted a non-standard function (IF) for my standard sql CASE expression ...
Ok sorry I didn't konw that, good to know.

Quote:
... you have needlessly double-quoted the column aliases ...
For me it's not needless because the alias can potentially contain spaces (that I never do but so anybody else can edit my query later)

Quote:
... you have needlessly parenthesized the join condition ...
For me it's not needless because the JOIN condition(s) can be multiple (and anyway it's the norm that I use for conditions in any langage)

Quote:
... the dangling comma
indeed.

Finally if we want to be purists maybe we should do that
Code:
SELECT
  articles.title
  ,
  sum(
    case
      when comments.status=1
        then 1
      else 0
    end) AS active
  ,
  sum(
    case
      when comments.status=0
        then 1
      else 0
    end) AS inactive
FROM
  articles
  LEFT JOIN comments
    ON articles.id = comments.parent 
GROUP 
    BY articles.title
Reply With Quote
  #6 (permalink)  
Old 03-21-06, 12:51
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
cool
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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