Link to home
Start Free TrialLog in
Avatar of Maxafi
Maxafi

asked on

Join syntax showing unusual results

Following on from a question I asked yesterday - (https://www.experts-exchange.com/questions/23499412/Add-an-additional-Join-to-an-existing-query.html) I now have a successful query as follows;


SELECT m.ID, m.shortname, m.retailer_name, o.merchantid, o.id AS oid, o.offertitle, o.offerbody, o.a_status, o.offerauthor, o.offershortname, o.clickcount, u.id, u.username
      FROM merchants m
    LEFT JOIN hurry_offers o
      ON m.id=o.merchantid
    LEFT JOIN hurry_users u
      ON o.offerauthor=u.ID
      ORDER BY oid DESC


I would now like to add a further join in order to get the number of comments against each 'offer'.  The query I currently have is;

SELECT m.ID, m.shortname, m.retailer_name, o.merchantid, o.id AS oid, o.offertitle, o.offerbody, o.a_status, o.offerauthor, o.offershortname, o.clickcount, u.id, u.username, count(c.offer_id) AS c_cnt, c.offer_id
      FROM merchants m
    LEFT JOIN hurry_offers o
      ON m.id=o.merchantid
    LEFT JOIN hurry_users u
      ON o.offerauthor=u.ID
    LEFT JOIN hurry_comments c
      ON o.id=c.offer_id
    GROUP BY c.offer_id
      ORDER BY o.id DESC

This works in as much as it is returning the correct figures for each element (i.e the correct username, comment count etc) however I'm only getting 2 results returned at the moment.  Unusually, there doesn't seem to be any reason that I can see for the 2nd of those results being returned - for example, it's not the 2nd row, it doesn't have any comments etc.

Having been working on this for some time, I'm hoping it's just something simple I've missed!

Any help would be much appreciated
Avatar of Tone' Shelby
Tone' Shelby
Flag of United States of America image

I know you said it's not the second row etc, but is any of the data in any of the fields for ether result even slightly different morespecifically in the c.offerid field. Even one difference in a character  in the data could pull back 2 results icluding a blank space ..You may try Trimmiming the c.offerid just to be sure. I've had trailing invisible spaces cause me grief like this before..I'd check data differences and leading / trailing blank spaces in the data first.
Hope it helps..
Avatar of Maxafi
Maxafi

ASKER

Thanks for the input, tshel.

Unfortunately, trimming hasn't helped - same result.
Can you provide a Create Table statement with some sample data with the two records of data and I'll be happy to dig in and see what I can come up with...
Hello...checking in on you. How are you coming along. Do you need further assistance / information  on this...thanks!
Avatar of Maxafi

ASKER

Hi tshel,

Apologies for not replying sooner - I must have missed the email notifications.

I did eventually solve this by doing a subquery to get the comment count, which did the trick.
ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial