Solved

Add an additional Join to an existing query

Posted on 2008-06-19
4
178 Views
Last Modified: 2010-03-20
Hi Folks,

Joins really aren't my strong point, so I'd be grateful of any pointers with this one.

This is my existing query;

SELECT m.ID, m.shortname, m.retailer_name, o.merchantid, o.id AS oid, o.offertitle, o.offerbody, o.a_status, o.offerauthor
FROM merchants m
LEFT JOIN hurry_offers o
ON m.id=o.merchantid
WHERE o.a_status='approved'
ORDER BY oid DESC

I have a 3rd table called 'hurry_users', which I would like to join.  The 'o.offerauthor' in the existing query returns the ID of a row which matches the row in hurry_users - what I would like to do is bring that back so instead of just returning the ID, I can return the actual username (in a field called 'username').

Hope that makes sense!
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
  • 2
4 Comments
 
LVL 19

Accepted Solution

by:
elimesika earned 500 total points
ID: 21823624
HI

Here it is
SELECT m.ID, u.username, m.shortname, m.retailer_name, o.merchantid, o.id AS oid, o.offertitle, o.offerbody, o.a_status, o.offerauthor
FROM merchants m
LEFT JOIN hurry_offers o
ON m.id=o.merchantid
LEFT JOIN hurry_users u
ON m.offerauthor=u.offerauthor
 
WHERE o.a_status='approved'
ORDER BY oid DESC

Open in new window

0
 
LVL 19

Expert Comment

by:elimesika
ID: 21823652
HI

Sorry , this is the correct query
SELECT m.ID, u.username, m.shortname, m.retailer_name, o.merchantid, o.id AS oid, o.offertitle, o.offerbody, o.a_status, o.offerauthor
FROM merchants m, hurry_offers o ,  hurry_users u
WHERE m.id=o.merchantid and oofferauthor=u.offerauthor and o.a_status='approved'
ORDER BY oid DESC

Open in new window

0
 

Author Comment

by:Maxafi
ID: 21823655
Worked perfectly, thanks! :-)
0
 
LVL 2

Expert Comment

by:mayno224
ID: 21823688
Try this.  But first replace column 'hurr_users_id' with the column that matches a row in o.offerauthor.  Hope this helps!

SELECT m.ID, m.shortname, m.retailer_name, o.merchantid, o.id AS oid, o.offertitle, o.offerbody, o.a_status, o.offerauthor, hurry_users.username
FROM merchants m
LEFT JOIN hurry_offers o
ON m.id=o.merchantid
INNER JOIN hurry_users on hurry_users.hurr_users_id=o.offerauthor
WHERE o.a_status='approved'
ORDER BY oid DESC

For more info on joins, check out: http://w3schools.com/sql/sql_join.asp
0

Featured Post

Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

Question has a verified solution.

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

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…
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

624 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