Jump to content

Recommended Posts

Hi all,

I have this query, to get entries for my blog. Before now, I used to count comments in a seperate query, but thought I'd have a go at doing it using a join.

 

This is what I have:

"SELECT B.*, COUNT(C.`ID`) AS `CommentCount`
FROM `$tb_Blog` B
LEFT OUTER JOIN `$tb_Blog` C ON B.`ID`=C.`ID2` AND C.`Type`='comment'
WHERE B.`Type`='entry'
ORDER BY B.`ID` ASC"

ID is the id of the entry/comment and ID2 is the field for comments that relates to its entry's ID.

However, this only returns one entry with an incorrect amount of comments.

 

I really don't understand why...

 

Can anyone help? :)

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/162064-solved-odd-results/
Share on other sites

Omg I'm such a f*cking retard; I forgot to group by `ID`. Ffs I feel like punching myself sometimes.

 

It's always the small things.

 

Shouldn't you be using a plain LEFT JOIN, rather than an OUTER?

I believe the word OUTER is optional. So LEFT JOIN === LEFT OUTER JOIN.

 

that's good to know, I was never much of a MySQL guru.  I usually go with the trusty LEFT JOIN, but I really should do some more research and tests with the various joins.

Link to comment
https://forums.phpfreaks.com/topic/162064-solved-odd-results/#findComment-855357
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.