有没有办法检查查询本身的hstore中是否已存在值.
我必须每行存储各种值(每行是“项目”).
我需要能够检查id是否已存在于其中一个hstore行的数据库中,而不是首先选择所有内容并在PHP中执行循环等.
hstore似乎是唯一提供类似内容的数据类型,并且还允许您将该行的列选择为数组.
Hstore可能不是存储这类数据的最佳数据类型,但没有其他更好的可用.
整个项目使用9.2,我无法改变 – json是9.3.
解决方法
exists()函数测试是否存在密钥.确定密钥“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
svals()函数将值作为集合返回.您可以查询结果以确定是否存在特定值.
select * from (select test_id,svals(test_hs) vals from test) x where vals = 'Wibble';
hstore Operators and Functions
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'));
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。