Solved

Add an additional Join to an existing query

Posted on 2008-06-19
4
174 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 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Query Syntax Error 9 48
Strange msg in the SSMS pane 13 49
Oracle 10g - insert string with special characters 8 41
Get Duration of last Status Update 4 30
'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 …
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

910 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

22 Experts available now in Live!

Get 1:1 Help Now