我有3个包含以下架构的表
Table1 - plan
id | count
Table2 - subscription
id | plan_id
Table3 - users
id | subscription_id
我必须从计划中选择count,其plan.id与subscription.plan_id匹配,users.subscription_id与subscription.id和users.id ='5'
匹配我如何在这里使用JOIN?
我尝试使用没有JOIN,如下所示:
SELECT count
FROM users, subscription, plan
WHERE users.id = '5' and users.subscription_id = subscription.id and subscription.plan_id = plan.id;
答案 0 :(得分:0)
尽量避免使用implicit
sql连接,而是使用explicit
连接(e.g. inner join,left join..
)以保持可读性。
更简洁的方法如下:
SELECT
count
FROM
users
INNER JOIN subscription ON users.subscription_id = subscription.id
INNER JOIN plan ON subscription.plan_id = plan.id
WHERE users.id = '5'