Postgres命令中忽略逻辑运算符

问题描述 投票:0回答:1

我有一个左连接命令,它还检查连接表之间数据中的匹配字母和其他两个组合。 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'
sql postgresql left-join logical-operators
1个回答
0
投票

qazxsw poi优先于qazxsw poi。所以

AND

OR

你可能想要的地方

WHERE left(b.loc_id,1)=left(a.table_name,1)
OR (...) 
and b.account_number='12345678'

如图所示使用括号来解决此问题。

© www.soinside.com 2019 - 2024. All rights reserved.