I have the following tables set up:
t_users
table:
|----------------------------------|
|uid | name | email |
|----------------------------------|
|1 | Maria | [email protected] |
|2 | George | [email protected] |
|----------------------------------|
t_settings
table:
|---------------------------------|
|t_sid | name | default_value |
|---------------------------------|
|1 | uicolour | #f00 |
|2 | language | en |
|---------------------------------|
t_users_settings
table:
|--------------------|
|uid | t_sid | value |
|--------------------|
|2 | 1 | #000 |
|1 | 2 | es |
|--------------------|
What I want, is for each user, to be able to pull all of their custom settings, and also pull the default settings for anything that they haven't changed. I have the following query that works:
SELECT t_users.*, t_users_settings.*, t_settings.*
FROM t_users
LEFT JOIN t_users_settings ON t_users.uid = t_users_settings.t_uid
LEFT JOIN t_settings ON t_settings.t_sid != t_users_settings.t_sid
WHERE t_users_settings.t_sid IS NOT NULL
AND t_users_settings.t_uid IS NOT NULL
AND t_users.uid = '<input user id here>';
However, it appears that if I add more settings (Such as country), I then have to add another JOIN
and AND
statement. I think there is something missing something in the setup,
or I'm just not familiar enough with MySQL to format the query correctly.
Little help?