如何COALESCE时间戳列?

Ale*_*ber 1 postgresql timestamp postgresql-9.3

在PostgreSQL 9.3中,我有下表有2个时间戳:

create table pref_users (
        id varchar(32) primary key,
        first_name varchar(64) not null,
        last_name varchar(64),
        female boolean,
        avatar varchar(128),
        city varchar(64),
        mobile varchar(64),
        login timestamp default current_timestamp,
        logout timestamp,
        last_ip inet,
        vip timestamp,       /* XXX can be NULL */
        grand timestamp,     /* XXX can be NULL */
        mail varchar(256),
        green integer,
        red integer,
        medals integer not null default 0
);
Run Code Online (Sandbox Code Playgroud)

时间戳vipgrand指示我的游戏用户是否支付了某些特权 - 直到那些日期.

当用户连接到我的游戏服务器时,我使用OUT参数调用以下过程:

create or replace function pref_get_user_info(
    IN _id varchar,
    OUT is_banned boolean,
    OUT is_grand boolean,
    OUT is_vip boolean,
    OUT rep integer
) as $BODY$
        begin
            is_banned := exists(select 1 from pref_ban where id=_id);

            if is_banned then
                return;
            end if;

            select
                grand > current_timestamp,
                vip > current_timestamp,
            into is_grand is_vip
            from pref_users where id=_id;

            if is_grand or is_vip then
                return;
            end if;

            select
                count(nullif(nice, false)) -
                count(nullif(nice, true))
            into rep
            from pref_rep where id=_id;
        end;
$BODY$ language plpgsql;
Run Code Online (Sandbox Code Playgroud)

这确实很有效,但有时会将NULL值传递给我的游戏守护进程(对于Perl脚本):

# select * from pref_get_user_info('OK674418426646');
 is_banned | is_grand | is_vip | rep
-----------+----------+--------+-----
 f         |          |        | 126
(1 row)
Run Code Online (Sandbox Code Playgroud)

我不需要一个NULL(它在我的Perl脚本中打印一个警告) - 我只需要一个"真"或"假"值.

所以我试过了:

select
    coalesce(grand, 0) > current_timestamp,
    coalesce(vip, 0) > current_timestamp,
into is_grand is_vip
from pref_users where id=_id;
Run Code Online (Sandbox Code Playgroud)

但这给了我错误:

# select * from pref_get_user_info('OK674418426646');
ERROR:  COALESCE types timestamp without time zone and integer cannot be matched
LINE 2:                         coalesce(grand, 0) > current_timesta...
                                                ^
QUERY:  select
                        coalesce(grand, 0) > current_timestamp,
                        coalesce(vip, 0) > current_timestamp,
                                  is_vip
                    from pref_users where id=_id
CONTEXT:  PL/pgSQL function pref_get_user_info(character varying) line 9 at SQL statement
Run Code Online (Sandbox Code Playgroud)

所以我想知道该怎么办?

我真的需要吗?

select
    coalesce(grand, current_timestamp - interval '1 day') > current_timestamp,
    coalesce(vip, current_timestamp - interval '1 day') > current_timestamp,
into is_grand is_vip
from pref_users where id=_id;
Run Code Online (Sandbox Code Playgroud)

或者是否有更好的方式(比如"epoch start"或"昨天")?

更新:

正如Clodoaldo Neto所建议的那样(谢谢!)我试过:

select
coalesce(grand > current_timestamp, false),
coalesce(vip > current_timestamp, false),
into is_grand is_vip
from pref_users where id=_id;
Run Code Online (Sandbox Code Playgroud)

但是is_vip,当为NULL vip是NULL:

# select * from pref_get_user_info('OK674418426646');
 is_banned | is_grand | is_vip | rep
-----------+----------+--------+-----
 f         | t        |        |
(1 row)
Run Code Online (Sandbox Code Playgroud)

当我尝试以下任何一种时,我得到语法错误:

select
coalesce(grand > current_timestamp, false),
coalesce(vip > current_timestamp, false),
into is_grand, is_vip
from pref_users where id=_id;

select
coalesce(grand > current_timestamp, false),
coalesce(vip > current_timestamp, false),
into (is_grand, is_vip)
from pref_users where id=_id;
Run Code Online (Sandbox Code Playgroud)

我怎样才能一次SELECT变成2个变量?

Clo*_*eto 5

如果你想要一个布尔值:

coalesce(grand > current_timestamp, false)
Run Code Online (Sandbox Code Playgroud)

如果你需要0或1:

coalesce((grand > current_timestamp)::integer, 0)
Run Code Online (Sandbox Code Playgroud)

在更新的问题中,选择列表和into子句之间有一个额外的逗号

coalesce(vip > current_timestamp, false),
into is_grand, is_vip
Run Code Online (Sandbox Code Playgroud)

拿出来

coalesce(vip > current_timestamp, false)
into is_grand, is_vip
Run Code Online (Sandbox Code Playgroud)