Jump to content


Photo

Querying Multiple Tables


  • Please log in to reply
14 replies to this topic

#1 adamjnz

adamjnz
  • Members
  • PipPipPip
  • Advanced Member
  • 30 posts

Posted 10 October 2006 - 01:35 AM

Hey Guys,

I downloaded a tutorial on joining MySQL tables in a query from DMXZone however what I am trying to do goes slightly past the scope of that tutorial. I have tried playing around with the SQL query I want but cannot get it to work. If someone has some insight into this it would be much appreciated.

The query as I was trying it:
SELECT BlogPostID, ImgSrc, PostTitle, PostText, DateTime, Category, Deleted, User.Name, COUNT(`BlogPostComment.BlogCommentID`) AS `CommentCount`
FROM BlogPost JOIN User USING (userID) JOIN BlogPostComment USING (BlogPostID)
WHERE BlogPost.Deleted = '0'

Essentially what I am trying to do is this:
1) Pull the blog post data out of the 'BlogPost' Table
2) Crossreference the UserID stored in the 'BlogPost' table to pull out the Full Name of the Author from the 'User' Table
3) Count the number of comments for that particular blog post from the 'BlogPostComment' table using the BlogPostID


I can get 1 and 2 working togeather fine, but when I try and add in step 3 it all goes pear shaped (including telling me that DateTime is not ambiguous  ???)

#2 adamjnz

adamjnz
  • Members
  • PipPipPip
  • Advanced Member
  • 30 posts

Posted 10 October 2006 - 02:07 AM

UPDATE:

After fiddling with the code I can finally get it to run something without errors, however there are 2 problems:(

1) It will only return a Name from User.Name for the first result
2) The comment count is an aggregate for all Blog Posts, where I want it to be for each Blog Post

Any ideas?

SELECT BlogPost.BlogPostID, ImgSrc, PostTitle, PostText, BlogPost.DateTime, Category, Deleted, User.Name, COUNT(BlogPostComment.BlogCommentID) AS CommentCount
FROM BlogPost JOIN User USING (userID) RIGHT JOIN BlogPostComment ON BlogPostComment.BlogPostID = BlogPost.BlogPostID
WHERE BlogPost.Deleted = '0'
GROUP BY BlogPostID


#3 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 10 October 2006 - 03:30 PM

Very strange looking query... I'm not sure what you mean by (1); as for (2), you have what looks like the right GROUP BY statement, so I'm confused.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#4 adamjnz

adamjnz
  • Members
  • PipPipPip
  • Advanced Member
  • 30 posts

Posted 11 October 2006 - 12:48 AM

Very strange looking query... I'm not sure what you mean by (1); as for (2), you have what looks like the right GROUP BY statement, so I'm confused.


Ok, so for 1) it should be returning the Name of the person who wrote the post, this is got by taking the UserID and looking it up in the User table and returning the associated username. However, it is only returning a result for the 1st record returned by the query, all other results this feild returns nothing.

2) This is working but it is giving me the total for all blog posts, not just the blog post the comments are related to.

Hope this makes sense

#5 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 11 October 2006 - 07:50 PM

1) Probably because the JOIN condition is failing, so you're getting NULLs for the users.* columns.

2) I'm not sure how that could be, unless you're getting back a strange result set.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#6 adamjnz

adamjnz
  • Members
  • PipPipPip
  • Advanced Member
  • 30 posts

Posted 11 October 2006 - 11:34 PM

I will try and put it in better context, here is what I am trying to do:

1)       Pull BlogPostID, ImgSrc, PostTitle, PostText, BlogPost.DateTime, Category, Deleted values from the BlogPost table

2)       Pull the Name value from the User table based on the UserID stored against the record in the BlogPost table.

3)       Calculate the number of comments linked to a BlogPost from the BlogPostComment table based on the BlogPostID stored in the BlogPost table.

The following query works fine for points 1 & 2 but when I try and add point 3 into the mix with the 2nd query it does the following to the results:

1)       Adds up the total comments for all blog posts and return the value to each record returned

2)       Stop returning the Name for the User table for all but the first record returned.

Points 1 & 2:

SELECT BlogPost.BlogPostID, ImgSrc, PostTitle, PostText, BlogPost.DateTime, Category, Deleted, User.Name
FROM BlogPost JOIN User USING (userID)
WHERE BlogPost.Deleted = '0'

Returns something like:


 
   
   
   
   
   
   
   
   
   
 
 
   
   
   
   
   
   
   
   
   
 
 
   
   
   
   
   
   
   
   
   
 
 
   
   
   
   
   
   
   
   
   
 
Record BlogPostID ImgSrc PostTitle PostText DateTime Category Deleted Name
1 00001 Post1.jpg Title 1 Text 1… 2006-10-10…. Other 0 Adam Jobbins
2 00002 Post2.jpg Title 2 Text 2… 2006-10-10…. Announcements 0 Test User 2
N 0000N PostN.jpg Title N Text N… 2006-10.. Whatever 0 Users Name


Points 1, 2 & 3:

SELECT BlogPost.BlogPostID, ImgSrc, PostTitle, PostText, BlogPost.DateTime, Category, Deleted, User.Name, COUNT(BlogPostComment.BlogCommentID) AS CommentCount
FROM BlogPost JOIN User USING (userID) RIGHT JOIN BlogPostComment ON BlogPost.BlogPostID = BlogPostComment.BlogPostID
WHERE BlogPost.Deleted = '0'
GROUP BY BlogPostID

Returns something like: (2 Records Total in the BlogPostComment table, both linked to BlogPostID = 00001)


 
   
   
   
   
   
   
   
   
   
   
 
 
   
   
   
   
   
   
   
   
   
   
 
 
   
   
   
   
   
   
   
   
   
   
 
 
   
   
   
   
   
   
   
   
   
   
 
Record BlogPostID ImgSrc PostTitle PostText DateTime Category Deleted Name CommentCount
1 00001 Post1.jpg Title 1 Text 1… 2006-10-10…. Other 0 Adam Jobbins 2
2 00002 Post2.jpg Title 2 Text 2… 2006-10-10…. Announcements 0 2
N 0000N PostN.jpg Title N Text N… 2006-10.. Whatever 0 2


SHOULD RETURN:


 
   
   
   
   
   
   
   
   
   
   
 
 
   
   
   
   
   
   
   
   
   
   
 
 
   
   
   
   
   
   
   
   
   
   
 
 
   
   
   
   
   
   
   
   
   
   
 
Record BlogPostID ImgSrc PostTitle PostText DateTime Category Deleted Name CommentCount
1 00001 Post1.jpg Title 1 Text 1… 2006-10-10…. Other 0 Adam Jobbins 2
2 00002 Post2.jpg Title 2 Text 2… 2006-10-10…. Announcements 0 Test User 2 0
N 0000N PostN.jpg Title N Text N… 2006-10.. Whatever 0 Users Name N


#7 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 12 October 2006 - 12:51 AM

OK... I'll take a closer look; thanks for the details.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#8 adamjnz

adamjnz
  • Members
  • PipPipPip
  • Advanced Member
  • 30 posts

Posted 21 October 2006 - 09:40 AM

A friend at work (An SQL Guru of sorts) suggested the following query, however he used MS SQL and MySQL Doesnt seem to like the subquery (in the brackets) and throws an error...

---------------------------------------------------------------------------------------------
SELECT
BlogPost.BlogPostID
, ImgSrc
, PostTitle
, PostText
, BlogPost.DateTime
, Category
, Deleted
, User.Name
, x.commentcount AS CommentCount

FROM BlogPost
JOIN User on BlogPost.userID=User.userID
LEFT JOIN select(BlogPostID, count(*) as commentcount from BlogPostComment) x
ON BlogPost.BlogPostID = x.BlogPostID
WHERE BlogPost.Deleted = '0'

GROUP BY BlogPost.BlogPostID
ORDER BY BlogPost.BlogPostID

-------------------------------------------------------------------------------------

Comments? Thoughts?

#9 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 22 October 2006 - 08:17 PM

Sorry I never got back to this... that query may work, but the "subquery" is really just a unnamed view a.k.a. derived table, which should be fine.  But only if the word select is inside the parentheses -- otherwise, you'll get a syntax error on all versions.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#10 adamjnz

adamjnz
  • Members
  • PipPipPip
  • Advanced Member
  • 30 posts

Posted 23 October 2006 - 01:04 AM

Sorry I never got back to this... that query may work, but the "subquery" is really just a unnamed view a.k.a. derived table, which should be fine.  But only if the word select is inside the parentheses -- otherwise, you'll get a syntax error on all versions.


I tired putting select in the brackets but still getting exactly the same error message. Query was:

SELECT
BlogPost.BlogPostID
, ImgSrc
, PostTitle
, PostText
, BlogPost.DateTime
, Category
, Deleted
, User.Name
, x.commentcount AS CommentCount

FROM BlogPost

JOIN User on BlogPost.userID=User.userID
 
LEFT JOIN (select BlogPostID, count(*) as commentcount from

BlogPostComment) x
      ON BlogPost.BlogPostID = x.BlogPostID

WHERE BlogPost.Deleted = '0'

GROUP BY BlogPost.BlogPostID
ORDER BY BlogPost.BlogPostID

#11 MCP

MCP
  • Members
  • PipPipPip
  • Advanced Member
  • 60 posts

Posted 23 October 2006 - 03:16 AM

I think you just need to add all the fields to the group by. try this:

SELECT BlogPost.BlogPostID, ImgSrc, PostTitle, PostText, BlogPost.DateTime, Category, Deleted, User.Name, COUNT(BlogPostComment.BlogCommentID) AS CommentCount
FROM BlogPost JOIN User USING (userID) RIGHT JOIN BlogPostComment ON BlogPost.BlogPostID = BlogPostComment.BlogPostID
WHERE BlogPost.Deleted = '0'
GROUP BY BlogPost.BlogPostID, ImgSrc, PostTitle, PostText, BlogPost.DateTime, Category, Deleted, User.Name


#12 adamjnz

adamjnz
  • Members
  • PipPipPip
  • Advanced Member
  • 30 posts

Posted 23 October 2006 - 05:31 AM

This has the same result as originally reported in the thread.

#13 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 24 October 2006 - 02:33 AM

And what version is this?
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#14 adamjnz

adamjnz
  • Members
  • PipPipPip
  • Advanced Member
  • 30 posts

Posted 24 October 2006 - 07:24 PM

4.0.27-standard

#15 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 24 October 2006 - 08:51 PM

Maybe you can't use derived tables in <4.1... I'll have to check.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users