[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Need help writing mysql query that joins data from 3 different tables

Posted on 2012-08-24
14
Medium Priority
?
695 Views
Last Modified: 2012-08-31
I need some help writing a mysql query that joins data from 4 different tables.

This is what I currently have (where I'm retrieving and linking data from 2 different tables using "LEFT JOIN"):

SELECT 
seo_clientrequests.id,
seo_clientrequests.client_id,
seo_clientrequests.subject,
seo_clientrequests.private,
seo_clientrequests.project_type,
seo_clientrequests.status,
seo_clientrequests.submitted_date,
seo_clientrequests.last_updated,
seo_clientrequests.due_date,
users.companyname,
users.usertype,
users.whitelabel_company_id 
FROM seo_clientrequests 
LEFT JOIN users 
ON seo_clientrequests.client_id = users.id 
WHERE users.status = '1'

Open in new window


How would I update this query so that it also retrieves data from a 3rd table named "seo_projects" ?

Here are the columns that are in my seo_projects table:

seo_projects.ID,
seo_projects.client_id,
seo_projects.package_id,
seo_projects.project_start_date,
seo_projects.project_due_date,
seo_projects.project_url,
seo_projects.project_desc,
seo_projects.percent_complete,
seo_projects.waiting_on_reply,
seo_projects.email_chain_exults_client,
seo_projects.email_chain_wl_client,
seo_projects.email_chain_wl,
seo_projects.last_updated,
seo_projects.entered

The LEFT JOIN statement for this would be pretty much identical to the one I'm already using, though I can't seem to figure out how to update the query with a 2nd LEFT JOIN statement, .. or if that's even the correct approach:

LEFT JOIN users ON seo_projects.client_id = users.id

Open in new window



The goal would be for me to retrieve a DISTINCT listing of rows for both the "seo_clientrequests" & "seo_projects" tables (ie: if there are 5 rows in the seo_client requests table and 4 rows in the seo_projects table .. my query should return 10 rows), and to link both of them to the "users" table (so I can retrieve the companyname, usertype, and whitelabel_company_id values).

Please advise.

Thanks,
- Yvan
0
Comment
Question by:egoselfaxis
  • 7
  • 7
14 Comments
 
LVL 8

Expert Comment

by:Barry62
ID: 38331343
SELECT 
seo_clientrequests.id,
seo_clientrequests.client_id,
seo_clientrequests.subject,
seo_clientrequests.private,
seo_clientrequests.project_type,
seo_clientrequests.status,
seo_clientrequests.submitted_date,
seo_clientrequests.last_updated,
seo_clientrequests.due_date,
users.companyname,
users.usertype,
users.whitelabel_company_id 
seo_projects.ID,
seo_projects.package_id,
seo_projects.project_start_date,
seo_projects.project_due_date,
seo_projects.project_url,
seo_projects.project_desc,
seo_projects.percent_complete,
seo_projects.waiting_on_reply,
seo_projects.email_chain_exults_client,
seo_projects.email_chain_wl_client,
seo_projects.email_chain_wl,
seo_projects.last_updated,
seo_projects.entered
FROM seo_clientrequests 
LEFT JOIN users 
ON seo_clientrequests.client_id = users.id 
LEFT JOIN seo_projects
ON seo_clientrequests.id = seo_projects.client_id
WHERE users.status = '1'
                                  

Open in new window

0
 

Author Comment

by:egoselfaxis
ID: 38336202
Nope .. that doesn't work.  

First, .. I had to add in a missing comma, .. and then I tried changing ...

"ON seo_clientrequests.id = seo_projects.client_id"

to ...

"ON seo_clientrequests.client_id = seo_projects.client_id"

... but I am still returning the incorrect number of rows.  

The seo_clientrequests table contains 17 rows, .. and the seo_projects table contains 3 rows, .. but for some reason the query is returning 27 rows.

- yg
0
 
LVL 8

Expert Comment

by:Barry62
ID: 38336585
OK,

start the query with "SELECT DISTINCT" and change the left join of the users table to an inner join.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:egoselfaxis
ID: 38337435
When I apply the revisions to you've suggested, I return 37 rows.

SELECT DISTINCT 
seo_clientrequests.id,
seo_clientrequests.client_id,
seo_clientrequests.subject,
seo_clientrequests.private,
seo_clientrequests.project_type,
seo_clientrequests.status,
seo_clientrequests.submitted_date,
seo_clientrequests.last_updated,
seo_clientrequests.due_date,
users.companyname,
users.usertype,
users.whitelabel_company_id, 
seo_projects.ID,
seo_projects.package_id,
seo_projects.project_start_date,
seo_projects.project_due_date,
seo_projects.project_url,
seo_projects.project_desc,
seo_projects.percent_complete,
seo_projects.waiting_on_reply,
seo_projects.email_chain_exults_client,
seo_projects.email_chain_wl_client,
seo_projects.email_chain_wl,
seo_projects.last_updated,
seo_projects.entered
FROM seo_clientrequests 
INNER JOIN users 
ON seo_clientrequests.client_id = users.id 
LEFT JOIN seo_projects
ON seo_clientrequests.client_id = seo_projects.client_id
WHERE users.status = '1'

Open in new window

0
 
LVL 8

Expert Comment

by:Barry62
ID: 38337454
Sorry, I meant to also say switch around seo_projects and users

LEFT JOIN seo_projects
ON seo_clientrequests.client_id = seo_projects.client_id
INNER JOIN users 
ON seo_clientrequests.client_id = users.id 
WHERE users.status = '1'

Open in new window

0
 

Author Comment

by:egoselfaxis
ID: 38337475
That still returns 37 rows.
0
 
LVL 8

Expert Comment

by:Barry62
ID: 38337798
this is tricky.  Try a group by clause at the end:

GROUP BY seo_clientrequests.id,seo_projects.ID

Open in new window

0
 

Author Comment

by:egoselfaxis
ID: 38338127
No difference .. still getting 37 rows.
0
 
LVL 8

Expert Comment

by:Barry62
ID: 38338640
could you give me a sample of your data?  I can run some tests.
0
 

Author Comment

by:egoselfaxis
ID: 38349893
I actually decided on using the "UNION ALL" approach :

SELECT  
users.companyname,
users.usertype,
users.whitelabel_company_id,
seo_clientrequests.ID AS theid,
seo_clientrequests.project_type AS the_project_type,
seo_clientrequests.due_date AS the_due_date,
seo_clientrequests.status AS status_percent_complete,
seo_clientrequests.subject AS subject_package_id,
seo_clientrequests.private AS private_waiting_on_reply,
seo_clientrequests.client_id,
seo_clientrequests.last_updated
FROM seo_clientrequests 
LEFT JOIN users ON seo_clientrequests.client_id = users.id 
WHERE users.status = '1' 
ORDER BY seo_clientrequests.last_updated DESC) 
UNION ALL 
(SELECT 
users.companyname,
users.usertype,
users.whitelabel_company_id,
seo_projects.ID AS theid,
seo_projects.project_url AS the_project_type,
seo_projects.project_due_date AS the_due_date,
seo_projects.percent_complete AS status_percent_complete,
seo_projects.package_id AS subject_package_id,
seo_projects.waiting_on_reply AS private_waiting_on_reply,
seo_projects.client_id,
seo_projects.last_updated
FROM seo_projects 
LEFT JOIN users ON seo_projects.client_id = users.id 
WHERE users.status = '1' 
ORDER BY seo_projects.last_updated DESC)

Open in new window


The problem I'm now having is that I can't seem to figure out how to do an "ORDER BY" clause on the whole result set.  It seems I'm only able to do them to the 2 separate queries individually, .. which isn't working right.  

Any ideas as to how I might accomplish this?

Thanks,
- Yvan
0
 

Author Comment

by:egoselfaxis
ID: 38350085
Nevermind .. I don't know what I was thinking.  I have it working now:

(SELECT  

users.companyname,
users.usertype,
users.whitelabel_company_id,
seo_clientrequests.ID AS theid,
seo_clientrequests.project_type AS the_project_type,
seo_clientrequests.due_date AS the_due_date,
seo_clientrequests.status AS status_percent_complete,
seo_clientrequests.subject AS subject_package_id,
seo_clientrequests.private AS private_waiting_on_reply,
seo_clientrequests.client_id,
seo_clientrequests.last_updated

FROM seo_clientrequests 

LEFT JOIN users ON seo_clientrequests.client_id = users.id 

WHERE users.status = '1') 

UNION ALL 

(SELECT 

users.companyname,
users.usertype,
users.whitelabel_company_id,
seo_projects.ID AS theid,
seo_projects.project_url AS the_project_type,
seo_projects.project_due_date AS the_due_date,
seo_projects.percent_complete AS status_percent_complete,
seo_projects.package_id AS subject_package_id,
seo_projects.waiting_on_reply AS private_waiting_on_reply,
seo_projects.client_id,
seo_projects.last_updated

FROM seo_projects 

LEFT JOIN users ON seo_projects.client_id = users.id 

WHERE users.status = '1')  

ORDER BY last_updated DESC 

Open in new window

0
 
LVL 8

Accepted Solution

by:
Barry62 earned 1500 total points
ID: 38350224
So did I help in any way?
0
 

Author Comment

by:egoselfaxis
ID: 38354010
Well, .. to be honest, .. not really.  

But your efforts were sincere and appreciated, .. so I will award them accordingly :)

Thanks - and have a great weekend!

- Yvan
0
 
LVL 8

Expert Comment

by:Barry62
ID: 38356234
Well, I tried.  And, btw, I was the only one who did.  Thanks for the points.
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

In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
In this article, I’ll talk about multi-threaded slave statistics printed in MySQL error log file.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month19 days, 18 hours left to enroll

873 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