Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Add an additional Join to an existing query

Posted on 2008-06-19
4
Medium Priority
?
184 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
  • 2
4 Comments
 
LVL 19

Accepted Solution

by:
elimesika earned 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
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…
Loops Section Overview
Suggested Courses

824 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