Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql Hstore使用通配符搜索值_Sql_Postgresql_Hstore - Fatal编程技术网

Sql Hstore使用通配符搜索值

Sql Hstore使用通配符搜索值,sql,postgresql,hstore,Sql,Postgresql,Hstore,使用PostgreSQL,我希望能够在HStore字段中找到与给定查询匹配的所有键值对。例如,给定如下表: Name Parentage (Hstore) _____ ___________________ Rover Mother => Yellow Lab, Father => Black Lab Fido Mother => Black Lab, Father => Rottweiler Rex M

使用PostgreSQL,我希望能够在HStore字段中找到与给定查询匹配的所有键值对。例如,给定如下表:

Name        Parentage (Hstore)
_____       ___________________
Rover       Mother => Yellow Lab, Father => Black Lab
Fido        Mother => Black Lab, Father => Rottweiler
Rex         Mother => Labrador Retriever, Father => Springer Spaniel 
Lassie      Mother => Border Collie, Father => Collie
我如何查询任何在其家谱中有“%lab%”的狗?也就是说,搜索会找到罗孚、菲多和雷克斯,但不会找到莱西。我看到的唯一例子是在单个键中进行搜索-我需要在允许通配符的所有值中进行搜索

我已经研究过类似的问题,但它似乎只搜索一个特定的键,而不是在所有键的Hstore字段中找到的所有值


请注意,这是一个构造的示例,我试图使其可访问。在我的实际数据库中,我有语言代码的键,后跟不同语言中相同单词的翻译值。我需要能够进行搜索,可以命中任何值,而不管它使用的是哪种语言。

将存储区分成名称/父项行,然后选择distinct 父列与搜索条件匹配的狗的名称

根据实际数据的性质,这可能需要额外的数据 索引。我不会用hstore来做这个,但你的实际数据可能会 要与众不同

% psql -qe -f hstore.sql
begin;
create extension hstore;
create temp table dogs (
    "name" text,
    parentage hstore
);
insert into dogs values
('Rover', 'Mother => "Yellow Lab",Father => "Black Lab"')
,('Fido', 'Mother => "Black Lab",Father => "Rottweiler"')
,('Rex', 'Mother => "Labrador Retriever",Father => "Springer Spaniel"')
,('Lassie', 'Mother => "Border Collie",Father => "Collie"')
;
table dogs;
  name  |                          parentage                           
--------+--------------------------------------------------------------
 Rover  | "Father"=>"Black Lab", "Mother"=>"Yellow Lab"
 Fido   | "Father"=>"Rottweiler", "Mother"=>"Black Lab"
 Rex    | "Father"=>"Springer Spaniel", "Mother"=>"Labrador Retriever"
 Lassie | "Father"=>"Collie", "Mother"=>"Border Collie"
(4 rows)

select * from dogs where "name" in
(select distinct "name" from (
            select "name", unnest(avals(parentage)) as parent
        ) as plist
        where parent ilike '%lab%'
);
 name  |                          parentage                           
-------+--------------------------------------------------------------
 Rover | "Father"=>"Black Lab", "Mother"=>"Yellow Lab"
 Fido  | "Father"=>"Rottweiler", "Mother"=>"Black Lab"
 Rex   | "Father"=>"Springer Spaniel", "Mother"=>"Labrador Retriever"
(3 rows)

rollback;