1

I have two tables, one with settingIDs and values of those settings for each userID and another with userIDs, their emails and user names.

I am trying to join the values of specific settings, the problem is that not all users have this specific setting tied to their ID, so I end up with less rows than I actually need.

Table 1
userID settingID settingValue

Table 2
userID userDOB userEmail userName 

My query looks like this:

SELECT u.userID, u.userEmail, s.settingValue
FROM users u
LEFT JOIN userSettings s ON u.userID = s.userID
WHERE s.settingID = 1

What do I need to do to get all of the users in the list?

1

2 Answers 2

3

Your where clause turns you left join into an inner join. Put the condition in the JOIN

SELECT u.userID, u.userEmail, s.settingValue
FROM users u
LEFT JOIN userSettings s ON u.userID = s.userID
                        AND s.settingID = 1
Sign up to request clarification or add additional context in comments.

Comments

0

you can use UNION ALL clause to combine two JOINS

SELECT  u.userID, u.userEmail, s.settingValue
     FROM users u
     LEFT userSettings s
     ON u.userID = s.userID
UNION ALL
     SELECT  u.userID, u.userEmail, s.settingValue
     FROM users u
     RIGHT userSettings s
     ON u.userID = s.userID

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.