Solved

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

Posted on 2014-03-12
4
529 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 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Showing random records from database 10 37
Mysql Left Join Case 10 56
php mysql check email already in database. (second check) 7 32
PHP Query return divisible by 3 3 18
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

831 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