简体   繁体   English

postgreSQL hstore如果包含值

[英]postgreSQL hstore if contains value

Is there a way to check if a value already exists in the hstore in the query itself. 有没有办法检查查询本身的hstore中是否已存在值。

I have to store various values per row ( each row is an "item"). 我必须每行存储各种值(每行是“项目”)。 I need to be able to check if the id already exists in database in one of the hstore rows without selecting everything first and doing loops etc in php. 我需要能够检查id是否已存在于其中一个hstore行的数据库中,而不是首先选择所有内容并在php中执行循环等。 hstore seems to be the only data type that offers something like that and also allows you to select the column for that row into an array. hstore似乎是唯一提供类似内容的数据类型,并且还允许您将该行的列选择为数组。

Hstore may not be the best data type to store data like that but there isn't anything else better available. Hstore可能不是存储这类数据的最佳数据类型,但没有其他更好的可用。

The whole project uses 9.2 and i cannot change that - json is in 9.3. 整个项目使用9.2,我无法改变 - json是9.3。

The exist() function tests for the existence of a key. exist()函数测试是否存在密钥。 To determine whether the key '42' exists anywhere in the hstore . 确定密钥“42”是否存在于hstore中的任何位置。 . .

select *
from (select test_id, exist(test_hs, '42') key_exists
      from test) x
where key_exists = true;
test_id  key_exists
--
2        t

The svals() function returns values as a set. svals()函数将值作为集合返回。 You can query the result to determine whether a particular value exists. 您可以查询结果以确定是否存在特定值。

select *
from (select test_id, svals(test_hs) vals
      from test) x
where vals = 'Wibble';

hstore Operators and Functions hstore运算符和函数


create table test (
  test_id serial primary key,
  test_hs hstore not null
);

insert into test (test_hs) values (hstore('a', 'b'));
insert into test (test_hs) values (hstore('42', 'Wibble'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM