Postgres 命令中忽略的逻辑运算符
Logical operator ignored in Postgres command
我有一个左连接命令,它还检查连接的 table 之间数据中的匹配字母和其他两个组合。 DISTINCT ON 是为了确保 location_plus 与右侧 table 的结果没有重复。为了测试我的结果,我添加了一个 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'
AND
优先于 OR
。所以
WHERE left(b.loc_id,1)=left(a.table_name,1)
OR (...)
and b.account_number='12345678'
是
WHERE left(b.loc_id,1)=left(a.table_name,1)
OR ((...) and b.account_number='12345678')
您可能想要的位置
WHERE (left(b.loc_id,1)=left(a.table_name,1) OR (...))
and b.account_number='12345678'
如图所示使用括号来解决此问题。
我有一个左连接命令,它还检查连接的 table 之间数据中的匹配字母和其他两个组合。 DISTINCT ON 是为了确保 location_plus 与右侧 table 的结果没有重复。为了测试我的结果,我添加了一个 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'
AND
优先于 OR
。所以
WHERE left(b.loc_id,1)=left(a.table_name,1)
OR (...)
and b.account_number='12345678'
是
WHERE left(b.loc_id,1)=left(a.table_name,1)
OR ((...) and b.account_number='12345678')
您可能想要的位置
WHERE (left(b.loc_id,1)=left(a.table_name,1) OR (...))
and b.account_number='12345678'
如图所示使用括号来解决此问题。