You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PostgreSQL WHERE子句返回错误结果问题排查求助

问题排查与修正

原WHERE子句的核心问题

  1. Road类型的区间范围错误:你需求中Road类型需要destination_to_paris介于end_buffer(1156)和start_point(1296)之间,但原代码错误地将下限设为start_buffer(1196),缩小了合法范围。同时逻辑分支的括号分组不明确,SQL中AND优先级高于OR,原写法的逻辑组合可能被错误解析,导致超出范围的Road记录被意外返回。
  2. 非Road类型的条件精度问题:原代码用destination_to_paris < origin.start_buffer做严格小于判断,若你需要包含边界值,应调整为<=或使用BETWEEN。

修正后的WHERE子句

根据你的需求,明确分组每个逻辑分支,修正Road类型的区间范围:

WHERE
(
  -- Road类型:介于end_buffer和start_point之间(包含两端)
  (destination.poitable_type = 'Road'
   AND destination_to_paris BETWEEN origin.end_buffer AND origin.start_point)
  OR
  -- 其他类型(Town/Discoveries等):介于end_buffer和start_buffer之间(包含两端)
  (destination.poitable_type <> 'Road'
   AND destination_to_paris BETWEEN origin.end_buffer AND origin.start_buffer)
)

若你需要严格介于两者之间(不包含边界),则替换BETWEEN为范围比较:

WHERE
(
  (destination.poitable_type = 'Road'
   AND destination_to_paris > origin.end_buffer 
   AND destination_to_paris < origin.start_point)
  OR
  (destination.poitable_type <> 'Road'
   AND destination_to_paris > origin.end_buffer 
   AND destination_to_paris < origin.start_buffer)
)

验证修正结果

使用修正后的条件,你的测试数据将只返回符合预期的记录:

  • Road类型:仅destination_to_paris=1280的记录(1156≤1280≤1296)
  • 非Road类型:无符合条件的记录(1256、1246均大于start_buffer=1196)

内容的提问来源于stack exchange,提问作者Ben

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 19:20:19