Sql Postgres-过滤掉单词,但排除另一种模式

Sql Postgres-过滤掉单词,但排除另一种模式,sql,postgresql,pattern-matching,Sql,Postgresql,Pattern Matching,我正在尝试提取具有类似“%asian%”的行。但我的问题是这包括白人 有人能帮我找到一个模式,包括任何标签,比如“%asian%”,但不包括白种人吗?我主要是在寻找一个优雅的解决方案。我已经有了一个解决方案,其中我有一个临时表,该表在结果集中包含“%asian%”,然后我删除任何带有caucasian的结果。它并不优雅,所以我正在寻找一个更简单的解决方案 以下是结果集的示例: label -------------------- WHITE/CAUCASIAN Asian/Pacif Isl H

我正在尝试提取具有类似“%asian%”的行。但我的问题是这包括白人

有人能帮我找到一个模式,包括任何标签,比如“%asian%”,但不包括白种人吗?我主要是在寻找一个优雅的解决方案。我已经有了一个解决方案,其中我有一个临时表,该表在结果集中包含“%asian%”,然后我删除任何带有caucasian的结果。它并不优雅,所以我正在寻找一个更简单的解决方案

以下是结果集的示例:

label
--------------------
WHITE/CAUCASIAN
Asian/Pacif Isl His
CAUCASIAN
ASIAN
我希望结果是

label
--------------------
Asian/Pacif Isl His
ASIAN
试试这个:

 WHERE label ilike '%asian%' and label not ilike '%caucasian%' ? 
--your table:

create table a (
  label text primary key
);

insert into a values 
('WHITE/CAUCASIAN'),
('Asian/Pacif Isl His'),
('CAUCASIAN'),
('ASIAN');

--a function to split your labels into text arrays (splits on forward slash or space):

create or replace function get_terms(text) returns text[] language sql as '
  select regexp_split_to_array(lower($1), ''[\/ ]'');
'

--create a functional index for fast lookup:
create index terms on a using gin (get_terms(label));

--find rows where there is an array overlap:
select * from a where get_terms(label) && array['asian'];
试试这个:

 WHERE label ilike '%asian%' and label not ilike '%caucasian%' ? 
--your table:

create table a (
  label text primary key
);

insert into a values 
('WHITE/CAUCASIAN'),
('Asian/Pacif Isl His'),
('CAUCASIAN'),
('ASIAN');

--a function to split your labels into text arrays (splits on forward slash or space):

create or replace function get_terms(text) returns text[] language sql as '
  select regexp_split_to_array(lower($1), ''[\/ ]'');
'

--create a functional index for fast lookup:
create index terms on a using gin (get_terms(label));

--find rows where there is an array overlap:
select * from a where get_terms(label) && array['asian'];

您可以尝试全文搜索,但postgres的解析器将a/b视为文件路径,因此在这种情况下没有帮助

Houari的回答很好,但在大桌子上会很慢

试试这个:

 WHERE label ilike '%asian%' and label not ilike '%caucasian%' ? 
--your table:

create table a (
  label text primary key
);

insert into a values 
('WHITE/CAUCASIAN'),
('Asian/Pacif Isl His'),
('CAUCASIAN'),
('ASIAN');

--a function to split your labels into text arrays (splits on forward slash or space):

create or replace function get_terms(text) returns text[] language sql as '
  select regexp_split_to_array(lower($1), ''[\/ ]'');
'

--create a functional index for fast lookup:
create index terms on a using gin (get_terms(label));

--find rows where there is an array overlap:
select * from a where get_terms(label) && array['asian'];

您可以尝试全文搜索,但postgres的解析器将a/b视为文件路径,因此在这种情况下没有帮助

Houari的回答很好,但在大桌子上会很慢

试试这个:

 WHERE label ilike '%asian%' and label not ilike '%caucasian%' ? 
--your table:

create table a (
  label text primary key
);

insert into a values 
('WHITE/CAUCASIAN'),
('Asian/Pacif Isl His'),
('CAUCASIAN'),
('ASIAN');

--a function to split your labels into text arrays (splits on forward slash or space):

create or replace function get_terms(text) returns text[] language sql as '
  select regexp_split_to_array(lower($1), ''[\/ ]'');
'

--create a functional index for fast lookup:
create index terms on a using gin (get_terms(label));

--find rows where there is an array overlap:
select * from a where get_terms(label) && array['asian'];

哪里有像“%asian%”和“caucasian”这样的标签?或者请给出更多明确的例子!我编辑了一些更清晰的例子,你想得到什么?啊,伙计,我很抱歉没有弄清楚。再次编辑。哪里的标签类似于“%asian%”而哪里的标签不类似于“%caucasian%”?哪里的标签类似于“%asian%”而哪里的标签类似于“caucasian”?或者请给出更多明确的例子!我编辑了一些更清晰的例子,你想得到什么?啊,伙计,我很抱歉没有弄清楚。再次编辑。其中标签i类似于“%asian%”,而标签i不类似于“%caucasian%”?这实际上是一个不错的实现。当我尝试提取其他种族时,它将非常有用。谢谢。这实际上是一个不错的实现。当我尝试提取其他种族时,它将非常有用。非常感谢。