Jump to content

[SOLVED] Odd Results...


jxrd

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.