Postgresql,选择空字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14172978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Postgresql, select empty fields
提问by Wine Too
I'm trying to get empty "text" fields from my table which I cleared manually with pgadmin. Initially in those fields was '' and I can query them like this:
我试图从我用 pgadmin 手动清除的表中获取空的“文本”字段。最初在这些字段中是 '' 我可以这样查询它们:
SELECT mystr, mystr1 FROM mytable WHERE mystr='' or mystr1=''
But that not work if I delete text from them and leave cells blank.
但是,如果我从它们中删除文本并将单元格留空,那将不起作用。
How to write query to get those '' and clear cells together in result? Or clear cells alone?
如何编写查询以将那些 '' 和清除单元格放在一起作为结果?还是单独清除细胞?
回答by wildplasser
SELECT mystr, mystr1
FROM mytable
WHERE COALESCE(mystr, '') = ''
OR COALESCE(mystr1, '') = ''
;
Explanation: the coalesce(a,b,c, ...)
function traverses the list a,b,c,...
from left to right and stops at the first non-null element. a,b,c
can be any expression (or constant), but must yield the same type (or be coercable to the same type).
说明:该coalesce(a,b,c, ...)
函数a,b,c,...
从左到右遍历列表并在第一个非空元素处停止。a,b,c
可以是任何表达式(或常量),但必须产生相同的类型(或可强制转换为相同的类型)。