Solved

sql query: Need to add Join to query that already has left join

Posted on 2014-03-12
4
516 Views
Last Modified: 2014-03-12
My query is working perfectly. It's gleaning login, email first name and last name from wordpress tables: users and usermeta.

I need to add the field: course_progress from table: wp_wpcw_user_courses using "user_id" to join everything. My issue is that I'm already using "user_id" to join users and usermeta.

Here's my query so far using users and usermeta:

SELECT

                    
					  wp_users.user_login,
					  MAX(CASE WHEN wp_usermeta.meta_key = 'first_name' then wp_usermeta.meta_value ELSE NULL END) as first_name,
                    MAX(CASE WHEN wp_usermeta.meta_key = 'last_name' then wp_usermeta.meta_value ELSE NULL END) as last_name,
					wp_users.user_email
                   

                    FROM wp_users

                    LEFT JOIN wp_usermeta
                    ON wp_users.ID = wp_usermeta.user_id

                    GROUP BY wp_users.user_login ORDER BY 3 asc

Open in new window

0
Comment
Question by:phillystyle123
4 Comments
 
LVL 74

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 225 total points
ID: 39924716
My issue is that I'm already using "user_id" to join users and usermeta.
So what's the problem? There's no limit on the number of times that a column can show up in JOIN clauses. You are simply applying set theory to your data. In this case, you will be joining all of the rows from each table together where there is a commonality between the user_id columns. SQL will take care of eliminating  the rows that don't fit this criteria.

Just add in the additional JOIN.
0
 
LVL 34

Accepted Solution

by:
Dan Craciun earned 225 total points
ID: 39924720
Like this:
SELECT wp_users.user_login,
		MAX(CASE WHEN wp_usermeta.meta_key = 'first_name' then wp_usermeta.meta_value ELSE NULL END) as first_name,
		MAX(CASE WHEN wp_usermeta.meta_key = 'last_name' then wp_usermeta.meta_value ELSE NULL END) as last_name,
		wp_users.user_email
	FROM wp_users
	LEFT JOIN wp_usermeta
	ON wp_users.ID = wp_usermeta.user_id
	LEFT JOIN wp_wpcw_user_courses 
	ON wp_users.ID = wp_wpcw_user_courses.user_id
	GROUP BY wp_users.user_login ORDER BY 3 asc

Open in new window

0
 
LVL 29

Assisted Solution

by:Paul Jackson
Paul Jackson earned 50 total points
ID: 39924721
You can use the same column to join multiple tables, I am assuming there is a user_id column in the wp_wpcw_user_courses table :

SELECT

                    
					  wp_users.user_login,
					  MAX(CASE WHEN wp_usermeta.meta_key = 'first_name' then wp_usermeta.meta_value ELSE NULL END) as first_name,
                    MAX(CASE WHEN wp_usermeta.meta_key = 'last_name' then wp_usermeta.meta_value ELSE NULL END) as last_name,
					wp_users.user_email
                   

                    FROM wp_users

                    LEFT JOIN wp_usermeta
                    ON wp_users.ID = wp_usermeta.user_id

                    LEFT JOIN wp_wpcw_user_courses 
                    ON wp_users.ID = wp_wpcw_user_courses.user_id

                    GROUP BY wp_users.user_login ORDER BY 3 asc 

Open in new window

0
 

Author Closing Comment

by:phillystyle123
ID: 39924770
thanks all -works perfectly - much appreciated
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The purpose of this video is to demonstrate how to reset a WordPress password if you are locked out and cannot reset the password. A typical use would be if you cannot access the email to which WordPress would send the password recovery email to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

743 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

9 Experts available now in Live!

Get 1:1 Help Now