WHERE语句在左外连接中的位置

时间:2017-04-03 08:21:13

标签: sql postgresql

在以下查询中,我首先尝试过滤所有network_id = 24的订阅,然后将列payment_plans.plan_type加入ids相同的订阅中。

我认为WHERE语句的定位存在问题,但我不知道它在哪里 - 错误显示连接语句附近存在语法错误

有关错误可能位置的任何建议吗?

SELECT DISTINCT subscriptions.user_id, subscriptions.payment_plan_id, payment_plans.plan_type
FROM subscriptions WHERE network_id = 24
LEFT OUTER JOIN payment_plans ON subscription.payment_plan_id = payment_plans.id


ERROR:  syntax error at or near "LEFT"
LINE 3: LEFT OUTER JOIN payment_plans ON subscription.payment_plan_i...
        ^

4 个答案:

答案 0 :(得分:2)

SQL语法为select ... from ... join .. on ..where

SELECT DISTINCT subscriptions.user_id, subscriptions.payment_plan_id, payment_plans.plan_type
FROM subscriptions 
LEFT OUTER JOIN payment_plans ON subscription.payment_plan_id = payment_plans.id
WHERE network_id = 24

这是SQL标准,对所有RDBMS都是一样的。

答案 1 :(得分:2)

您的查询应该是:

SELECT DISTINCT subscriptions.user_id, subscriptions.payment_plan_id, payment_plans.plan_type
FROM subscriptions 
LEFT OUTER JOIN payment_plans ON subscription.payment_plan_id = payment_plans.id
WHERE network_id = 24

您可以查看documentation for the SELECT statement, in particular this section

答案 2 :(得分:0)

在join语句后移动你的位置。像这样:

SELECT DISTINCT subscriptions.user_id, subscriptions.payment_plan_id, 
payment_plans.plan_type
FROM subscriptions 
LEFT OUTER JOIN payment_plans ON subscription.payment_plan_id = 
payment_plans.id 
WHERE network_id = 24

答案 3 :(得分:0)

你可以试试这个

SELECT DISTINCT subscriptions.user_id, subscriptions.payment_plan_id, payment_plans.plan_type FROM subscriptions LEFT OUTER JOIN payment_plans ON subscription.payment_plan_id = payment_plans.id WHERE subscriptions.network_id = 24