【发布时间】:2020-05-30 22:01:55
【问题描述】:
我尝试对我的 postgres 数据库运行以下查询:
select distinct offer_id from offers
where listing_id = 2299392
group by offer_id
having not bool_or(status in ('Rejected', 'Draft') or (status = 'Pending' and expiry_date < now())
or (seller_t_and_c_accepted = true and buyer_t_and_c_accepted = true and
( (seller_conditions = null and buyer_conditions = null) -- neither buyer nor seller have any conditions
-- seller has conditions and they are all done
or (buyer_conditions = null and not exists (select * from json_each(offers.seller_conditions::json) as sc(key,val) where (sc.val -> 'done')::text = 'false' or sc.val ? 'done'))
-- buyer has conditions and they are all done
or (seller_conditions = null and not exists (select * from json_each(offers.buyer_conditions::json) as bc(key,val) where (bc.val -> 'done')::text = 'false' or bc.val ? 'done'))
-- both buyer and seller have conditions and they are all done
or (not exists (select * from json_each(offers.buyer_conditions::json) as bc(key,val) where (bc.val -> 'done')::text = 'false' or sc.val ? 'done') and not exists (select * from json_each(offers.seller_conditions::json) as sc(key,val) where (sc.val -> 'done')::text = 'false' or sc.val ? 'done'))
)
)
)
我收到了错误:
operator does not exist: json ? unknown
当我将以下部分添加到上述查询中时会发生这种情况:
sc.val ? 'done'
...
bc.val ? 'done'
我不是 sql 或 postgres 专家,但我 认为 json_object ?如果 'field' 是 json 对象中存在的字段的名称,则 'field' 应该返回 true,否则返回 false(即,如果它不存在于 json 对象中)。
我认为你不需要知道我在上面的查询中要完成什么,但它可能会有所帮助,所以我会为你分解它。
我正在尝试获取给定房地产列表(在本例中为 ID 为 2299392 的列表)的所有有效报价。列表上的有效报价是未被拒绝、不在草稿、未过期和未完成的报价。 “完整”要约是指买卖双方均已接受条款和条件且买方的所有条件(如果有)均已满足且卖方的所有条件(如果有)均已满足的要约。
上面查询中“完整”报价的条件最令人费解,因此我将进一步分解。开头是这样的:
...or (seller_t_and_c_accepted = true and buyer_t_and_c_accepted = true and
买卖双方均已接受条款和条件...
( (seller_conditions = null and buyer_conditions = null) or ...
涵盖买卖双方均无条件的情况。
(buyer_conditions = null and not exists (select * from json_each(offers.seller_conditions::json) as sc(key,val) where (sc.val -> 'done')::text = 'false' or sc.val ? 'done')) or...
涵盖买方没有条件但卖方有条件的情况。条件存储为 json 对象:
{
"Subject to financing": {"date": "2020-03-19", "time": "2100", "done": false},
"Subject to inspection": {"date": "2020-03-19", "time": "2100", "done": true}
}
它本质上是一系列条件,每个条件本身都是一个json对象,由截止日期(日期和时间)以及是否满足(完成)组成。
为了通过这个解析看看是否满足所有条件,我有一个嵌套选择:
select * from json_each(offers.seller_conditions::json) as sc(key,val) where (sc.val -> 'done')::text = 'false' or sc.val ? 'done'
我正在使用 json_each 遍历所有条件并获取键/值对(因此键可能是“受融资约束”,值是 {"date": "2020-03-19", "time": " 2100”,“完成”:假})。然后我检查该值以查看“完成”字段是否为假(意味着此条件不满足,因此属于我的结果)。我还检查了该值是否有一个名为“完成”的字段(因为有时它不会,如:{“日期”:“2020-03-19”,“时间”:“2100”})。
不管怎样,其余的都差不多:
(seller_conditions = null and not exists (select * from json_each(offers.buyer_conditions::json) as bc(key,val) where (bc.val -> 'done')::text = 'false' or bc.val ? 'done')) or ...
涵盖卖方没有条件但买方有条件的情况,并且:
(not exists (select * from json_each(offers.buyer_conditions::json) as bc(key,val) where (bc.val -> 'done')::text = 'false' or sc.val ? 'done') and not exists (select * from json_each(offers.seller_conditions::json) as sc(key,val) where (sc.val -> 'done')::text = 'false' or sc.val ? 'done'))
涵盖买卖双方都有条件的情况。
无论如何,有没有人明白为什么它会告诉我?运营商不存在?我写错了吗?有没有另一种方式来完成我想要的?谢谢。
【问题讨论】:
标签: sql json postgresql