当我在 postgresql 中运行以下查询时,收到错误消息 ERROR: negative substring length not allowed:
select count(*) as name_matches
from table1 x
inner join
(select * from table2 where trim(last_name) <> 'unknown') y
using (id1)
where substring( trim(x.full_name), 1, (length( trim(x.full_name) ) - 2) ) = trim(y.last_name||' '||y.first_name)
但是,此查询返回 0 个结果:
select count(*)
from table1 x
inner join
(select * from table2 where trim(last_name) <> 'unknown') y
using (id1)
where length( trim(x.full_name) ) < 3
请注意,所有字段都是非空字段。对于可能被忽视的事情有什么建议吗?谢谢!
最佳答案
最后一个 WHERE
子句中的
x.full_name
和 x.last_name
在整个表上执行。查询优化器可以选择在连接之前或之后执行。在这种情况下,它似乎是在连接发生之前执行的,因此您会收到错误,因为尚未过滤掉会导致负子字符串长度的“未知”项。
关于sql - postgresql:不允许负子字符串长度,但数据中没有明显的违规,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39154661/