?
Solved

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

Posted on 2014-03-12
4
Medium Priority
?
545 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 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 900 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 35

Accepted Solution

by:
Dan Craciun earned 900 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 200 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

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 blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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 Month13 days, 14 hours left to enroll

809 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