Solved

Join syntax showing unusual results

Posted on 2008-06-20
7
180 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
  • 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In database programming, custom sort order seems to be necessary quite often, at least in my experience and time here at EE. Within the realm of custom sorting is the sorting of numbers and text independently (i.e., treating the numbers as number…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This video discusses moving either the default database or any database to a new volume.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now