有没有办法配置psql或在查询时传入一些参数,打印出空值和空字符串的不同值?目前,两者都显示为空。
例如,我有一个包含以下内容的表:
adventure=# select * from account
adventure-# ;
user_id | username | password | email | created_on | last_login
---------+----------+----------+-------+----------------------------+----------------------------
1 | | | | 2020-04-07 10:30:08.836098 | 2020-04-07 10:30:08.836098
(1 row)
这样,我就不能判断username是空字符串还是null。在本例中,用户名为空,但密码为空字符串。
我试过使用\x on
,但都是一样的。
当然,我可以做一些合并,但是如果我需要对每一列都这样做,那就有点太多了。
发布于 2020-04-07 18:51:24
在psql
中,您可以使用配置选项null
执行此操作,该选项可以使用\pset
进行更改
postgres=# \pset null '<null>'
Null display is "<null>".
postgres=# select null as c1, '' as c2;
c1 | c2
--------+----
<null> |
如果您想永久使用该选项,可以将\pset
命令放入.psqlrc中
发布于 2020-04-07 18:48:11
您可以使用coalesce()
将NULL
值替换为其他值:
select coalesce(username, '<null user>') as username,
coalesce(password, '<null password>') as password
或者,如果要为空字符串包含第三个值,请使用case
select (case when username is null then '<null user>'
when username = '' then '<empty user>'
else username
end)
当然,如果密码是<null password>'
,那么它看起来就像NULL
值。
或者,您可以将值括在引号中:
select '"' || username || '"' as username
如果值为NULL
,则||
运算符返回NULL
。
https://stackoverflow.com/questions/61078256
复制相似问题