【发布时间】:2018-08-12 23:55:15
【问题描述】:
我有一个左连接命令,它还检查连接表之间数据中的匹配字母和其他两个组合。 DISTINCT ON 是为了确保 location_plus 没有与右侧表中的结果重复。为了测试我的结果,我添加了一个 AND 来检查特定的帐号结果。这个 AND 组件似乎被完全忽略了,返回几乎所有的记录,而不是那个 a/c 号码的 ~10。这是为什么呢?
SELECT
DISTINCT ON (location_plus)
a.*,b.*
FROM schema.source_table b
LEFT JOIN schema.other_reference a
ON a.loc_id = (substring(b.loc_id,3))::integer
WHERE left(b.loc_id,1)=left(a.table_name,1)
OR ((left(b.loc_id,1)='B' AND left(a.table_name,1)='A') OR (left(b.loc_id,1)='B' AND left(a.table_name,1)='B'))
and b.account_number='12345678'
【问题讨论】:
-
因为您在没有正确括号的情况下将
OR与AND混合在一起。
标签: sql postgresql left-join logical-operators