gpt4 book ai didi

sql - rails query hstore where key IS NOT something 或不存在

转载 作者:行者123 更新时间:2023-12-04 22:11:25 26 4
gpt4 key购买 nike

我有一个带有 hstore 的表,有时我用它来存储复选框的信息。我们使用“1”表示选中,“0”表示未选中。 hstore 没有默认值,因此用户可以处于 4 种状态之一:

customer_a = Customer.new({properties: {checkbox: "1"}})   # checked
customer_b = Customer.new({properties: nil}) # unchecked(object doesn't exist)
customer_c = Customer.new({properties: {checkbox: "0"}}) # unchecked(unchecked)
customer_d = Customer.new({properties: {taco: "chicken"}}) # unchecked(key doesn't exist)

情况:我怎样才能找到所有“未检查”的行?

Customer.where(" customer.properties -> 'checkbox' NOT LIKE '1' ")

^^^ 不起作用^^^ 忽略“复选框”键为空且属性为空的客户

Customer.where("customers.properties -> 'checkbox' LIKE '%0%' OR customers.properties IS NULL")

^^^不起作用^^^也忽略了 key 丢失的地方

有没有更好的方法在单个查询中执行此操作?

查询应该返回 [customer_b, customer_c, customer_d]

目前的解决方案:- 检查:Customer.where("customer.properties -> 'checkbox' LIKE '1' ")- 未选中:Customer.all - Customer.where("customer.properties -> 'checkbox' LIKE '1' ")

是否有 sql 查询返回 hstore 键不存在的行?

最佳答案

如果要查找 hstore 为空的所有记录,请使用

Customer.where(properties: ['',nil]) 

Customer.where("properties='' OR properties IS NULL")

编辑

假设你想从你关于空字符串的评论中找到所有没有 1 的东西,你可以试试这个吗,

Customer.where("properties -> 'checkbox' NOT LIKE '1' OR NOT(properties ? 'checkbox') OR properties = '' OR properties is null")

检查hstore是否包含一个键

properties ? 'checkbox' 

检查包含,我正在做一个 not 来查找没有复选框的内容,然后是空属性。让我知道它是否有效。

关于sql - rails query hstore where key IS NOT something 或不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27805026/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com