开发者

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:

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. 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.

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. 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.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜