[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Join syntax showing unusual results

Posted on 2008-06-20
7
Medium Priority
?
188 Views
Last Modified: 2011-10-03
Following on from a question I asked yesterday - (http://www.experts-exchange.com/Programming/Languages/SQL_Syntax/Q_23499412.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
0
Comment
Question by:Maxafi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
7 Comments
 
LVL 9

Expert Comment

by:Tone' Shelby
ID: 21852060
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..
0
 

Author Comment

by:Maxafi
ID: 21868891
Thanks for the input, tshel.

Unfortunately, trimming hasn't helped - same result.
0
 
LVL 9

Expert Comment

by:Tone' Shelby
ID: 21879999
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...
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Expert Comment

by:Tone' Shelby
ID: 22196263
Hello...checking in on you. How are you coming along. Do you need further assistance / information  on this...thanks!
0
 

Author Comment

by:Maxafi
ID: 22348445
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.
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 22793294
PAQed with points refunded (500)

Computer101
EE Admin
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question