Solved

Add an additional Join to an existing query

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

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…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

760 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