简体   繁体   English

将3个MySQL表加入1个查询

[英]Joining 3 MySQL tables in to 1 query

I have 3 tables which store user, available settings and user settings. 我有3个表,用于存储用户,可用设置和用户设置。 User table has standard user stuff like username, password, name, date etc. Settings table has a list of all available system settings and a default value like: 用户表具有标准的用户资料,例如用户名,密码,名称,日期等。设置表具有所有可用系统设置的列表以及默认值,例如:

settingID   name                  defaultValue
----------------------------------------------
1           push notifications    1
2           profile image         0
3           full name             1
4           distance measure      Miles

User setting table has any custom settings set by the user like so: 用户设置表具有用户设置的任何自定义设置,如下所示:

userSettingID    userID    settingID    customValue
----------------------------------------------------------
1                1         1            0
2                1         3            0
3                1         4            KM

I am trying to make a query that will join all 3 tables together however produce ALL results from the settings table and if there is a match in the userSettings table use the customValue otherwise use the defaultValue from the settings table. 我正在尝试将所有3个表连接在一起的查询,但是会从设置表中产生所有结果,如果userSettings表中存在匹配项,请使用customValue,否则请使用设置表中的defaultValue。

So I would like to produce the below: 所以我想产生以下内容:

userID   settingID   userSettingID   name     defaultValue   customValue
---------------------------------------------------------------------------
1        1           1               push...  1              0
null     2           null            profi..  0              null
1        3           2               full...  1              0
1        4           3               dista..  Miles          KM

As you can see I would like EVERY setting record and if there are matches populate with userID and/or customValue otherwise fill with NULL. 如您所见,我想要每一个设置记录,如果有匹配项,则填充userID和/或customValue,否则填充NULL。

I was able to get the desired results but as soon as I add a WHERE userID = x clause I only get records that a user has been set to. 我能够获得期望的结果,但是一旦添加WHERE userID = x子句,我就只会获取已设置用户的记录。

Thanks 谢谢

Edit: The query I have so far is: 编辑:到目前为止,我的查询是:

SELECT *
FROM settings
LEFT OUTER JOIN userSettings ON settings.settingID = userSettings.settingID
LEFT OUTER JOIN user ON userSettings.userID = user.userID

To get the customValue instead of the defaultValue if it is present you can use the coalesce function and to filter by userid you'll want to add the condition to the join instead of using it in a where clause (which would make the left join behave like an inner join). 要获取customValue而不是defaultValue如果存在),可以使用coalesce函数并按userid进行过滤,您希望将条件添加到customValue而不是在where子句中使用它(这会使左联接的行为像是内部联接)。

I'm guessing you want something like this: 我猜你想要这样的东西:

select 
    us.userID, 
    s.settingID, 
    us.userSettingID,
    s.name, 
    s.defaultValue,
    us.customValue,
    coalesce(us.customValue, s.defaultvalue) as OverriddenValue
from settings s
left join Usersetting us on s.settingID = us.settingID 
left join user u on u.userid = us.userID and u.userID = 1

user is a non-reserved keyword in MySQL and as such isn't a good choice for an identifier, although it is permitted to be used without quoting. user是MySQL中不可保留的关键字,因此尽管允许使用不带引号的标识符,但这并不是标识符的好选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM