?
Solved

Add an additional Join to an existing query

Posted on 2008-06-19
4
Medium Priority
?
181 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 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

Quick Start: DOCKER

Sometimes you just need a Quick Start on a topic in order to begin using it.. this is just what you need to know to get up and running with Docker!

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…
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 course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

719 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