我在PostgreSQL 9.5中创建了一个表,如下所示,我添加了一些数据。但是,当我尝试通过不区分大小写的搜索来查询数据时,就像!〜*一样,它甚至会删除空值行。我怎么能做一个将返回所有null和蔬菜而不是水果的类别的查询?
CREATE TABLE temp
(
category character varying,
item character varying
);
INSERT INTO temp VALUES('Fruits', 'apple');
INSERT INTO temp VALUES('FRUITS', 'applE');
INSERT INTO temp(item) VALUES('Apple');
INSERT INTO temp(item) VALUES('BANANA');
INSERT INTO temp VALUES('Vegetables', 'Cabbage');
查询
Select * from temp where category !~* 'fruits'
输出
category item
-------- --------
Vegetables Cabbage
答案 0 :(得分:1)
要处理null,您可以使用is distinct from
:
Select *
from temp
where lower(category) is distinct from 'fruits'
或者如果你想要正则表达式:
Select *
from temp
where category !~* 'fruits'
or category is null;
或者将null视为其他内容:
Select *
from temp
where coalesce(category, '') !~* 'fruits'