- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个带有 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/
Postgres 新手,使用 v9.3,想利用 hstore。 当我尝试连接两个 hstore 值时,出现奇怪的错误: SELECT p.properties->'name' || p.propert
我有一个定期用新数据更新的汇总表。其中一列是 hstore 类型。当我更新新数据时,如果键存在,我想将键的值添加到键的现有值,否则我想将对添加到 hstore。 现有数据: id sum
我在数据库中有一个现有模型。我想用一个 hstore 字段来增强它。我安装了 hstore Postgres 扩展,django-hstore 应用程序,更改了 django 项目中的适当设置: SO
我正在查询 pgsql 数据库以查找在 hstore 字段中具有特定键的行: select DISTINCT from (select id, exist(data, ‘exercise_quiz’)
我通过以下方式在我的 MacOS X 上安装了 RubyOnRails 4.1.4 和 PostgreSQL: brew install postgresql 当我尝试迁移这个时: CREATE EX
当我运行 Django 服务器时,出现以下错误: class HStoreDescriptor(models.fields.subclassing.Creator): AttributeErr
我需要让 Hstore 为我正在处理的 Rails 应用程序工作。我正在运行 Linux Mint 18 和 psql(9.6.1,服务器 9.5.5)和 postgresql-contrib 9.6
在我的实际项目中,我正沉浸在“大数据”问题中。我们需要存储一个大数据库,其中读取性能比写入更重要(数据更改最少且受控)。 有数百万个寄存器,每个寄存器都有很多键/值信息。 我正在测试 noSQL 数据
我正在使用 Postgresql V 9.1: SELECT 'k=>name, v=>Nava Ratna Secondary School'::hstore; 此代码返回错误: ERROR: S
我想知道在 PostgreSQL 中是否可以遍历当前函数的所有可用输入参数并将这些参数 (key=>value) 附加到 hstore 变量。 hstore-key = 给定输入参数/参数的名称...
我在这里问了类似的问题:hstore value with space。并由用户解决:Clodoaldo Neto。现在我遇到了下一个包含单引号的字符串的情况。 SELECT 'k=>"name",
假设我有一个博客数据库,其中 posts 表将标签存储在 hstore 中。 键代表标签 ID,值是标签名称。 例如:1=>'测试', 56=>'SQL', 42=>'Java' 我想在带有标签过滤器
我发现这个查询: SELECT properties->'somekey' IS NOT DISTINCT FROM 'someValue' FROM myTable; 在 PostgreSQL 9.
我正在使用 PostgreSQL 9.4。我需要将子查询转换为 hstore。我有这个查询:select code, value_string from dir。它返回 code | value_s
我在决定使用哪种方法时遇到了一些麻烦。 我有几个实体“类型”,我们称它们为 A、B 和 C,它们共享一定数量的属性(大约 10-15 个)。我创建了一个名为 ENTITIES 的表,并为每个公共(pu
我想在 hstore 列中的特定键上创建唯一索引或约束(如果该键存在)。我希望能在另一个问题的某处找到答案: Practical limitations of expression indexes i
当我在不传递任何参数的 Django 模型中使用 hstore.DictionaryField() 并在 Djano 管理中注册我的模型时,我可以在管理界面中即时创建新的键值对行。 当我在模式模式下使
尝试将 hstore (postgreql) 中的值插入到更通用的表中 在我的汽车表中,我有这些字段 id fields (hstore) 我的存储表,我有这些字段 id key value car_
我想知道是否有人知道这个在 PostgreSQL 9.2 中的 hstore 列上的简单查询出了什么问题 查询在 pgAdmin 中运行 select attributeValue->"CODE_MU
假设我有一个定义如下的表: CREATE TABLE test ( values HSTORE NOT NULL ); 想象一下,我插入了几条记录,结果如下: values -----------
我是一名优秀的程序员,十分优秀!