[英]Joining 3 tables
First off, sorry if this is a near enough duplicate. 首先,对不起,如果这是足够的重复。 I've found this question , which nearly does what I want, but I couldn't wrap my head around how to alter it to my needs.
我已经找到了这个问题 ,几乎可以满足我的要求,但是我无法解决如何根据自己的需要进行调整。
I've got these 3 tables: 我有以下3张桌子:
cs_Accounts: +----+-----------------------------+-------------+ | id | email | username | +----+-----------------------------+-------------+ | 63 | jamasawaffles@googlil.com | jamwaffles2 | | 64 | jamwghghhfles@goomail.com | jamwaffles3 | | 65 | dhenddfggdfgetal-pipdfg.com | dhendu9411 | | 60 | jwapldfgddfgfffles.co.uk | jamwaffles | +----+-----------------------------+-------------+ cs_Groups: +----+-----------+------------+-------------+ | id | low_limit | high_limit | name | +----+-----------+------------+-------------+ | 1 | 0 | 0 | admin | | 2 | 1 | 50 | developer | | 3 | 76 | 100 | reviewer | | 4 | 51 | 75 | beta tester | | 5 | 1 | 50 | contributor | +----+-----------+------------+-------------+ cs_Permissions: +----+---------+----------+ | id | user_id | group_id | +----+---------+----------+ | 4 | 60 | 4 | | 3 | 60 | 1 | | 5 | 60 | 2 | | 6 | 62 | 1 | | 7 | 62 | 3 | +----+---------+----------+
I've been wrestling with a 3 way join for hours now, and I can't get the results I want. 我一直在努力进行3种方式的联接,这已经有几个小时了,但我无法获得想要的结果。 I'm looking for this behaviour: a row will be returned for every user from
cs_Accounts
where there is a row in cs_Permissions
that contains their ID and the ID of a group from cs_Groups
, as well as the group with the group_id
has a high_lmiit
and low_limit
in a range I can specify. 我在寻找这种行为:行会从每个用户返回
cs_Accounts
那里有一排cs_Permissions
包含其ID,并从一组ID cs_Groups
,以及与该组group_id
有high_lmiit
和low_limit
在我可以指定的范围内。
Using the data in the tables above, we might end up with something like this: 使用上表中的数据,我们可能会得到如下所示的结果:
email username cs_Groups.name ---------------------------------------------------------- jwapldfgddfgfffles.co.uk jamwaffles admin jwapldfgddfgfffles.co.uk jamwaffles developer jwapldfgddfgfffles.co.uk jamwaffles beta tester dhenddfggdfgetal-pipdfg.com dhendu9411 admin dhenddfggdfgetal-pipdfg.com dhendu9411 reviewer
There is an extra condition, however. 但是,还有一个额外的条件。 This condition is where rows are only selected if the group the user belongs to has a
high_limit
and low_limit
with values I can specify using a WHERE
clause. 在这种情况下,只有当用户所属的组的
high_limit
和low_limit
具有可以使用WHERE
子句指定的值时,才选择行。 As you can see, the table above only contains users with rows in the permissions table. 如您所见,上表仅包含在权限表中具有行的用户。
This feels a lot like homework but with a name like James I'm always willing to help. 这感觉很像家庭作业,但我总是很愿意提供帮助,例如James。
select a.email,a.username,g.name
from cs_Accounts a
inner join cs_Permissions p on p.user_id = a.id
inner join cs_Groups g on g.id = p.Group_id
where g.low_limit > 70
and g.high_limt < 120
This is the query 这是查询
SELECT ac.email, ac.username, gr.name
FROM cs_Accounts AS ac
LEFT JOIN cs_Permissions AS per ON per.user_id = ac.id
INNER JOIN cs_Groups AS gr ON per.user_id = gr.id
You can add a WHERE
clause to this query if you want 您可以根据需要在此查询中添加
WHERE
子句
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.