在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
);
时间戳vip
和grand
表示我的游戏用户是否已支付某些特权 - 直到这些日期为止。
当用户连接到我的游戏服务器时,我使用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;
这确实很有效,但有时会将NULL
值传递给我的游戏守护进程(对于Perl脚本):
# select * from pref_get_user_info('OK674418426646');
is_banned | is_grand | is_vip | rep
-----------+----------+--------+-----
f | | | 126
(1 row)
我不需要NULL
(并且它会在我的Perl脚本中打印警告) - 我只需要一个" true"或"假"价值观。
所以我尝试过:
select
coalesce(grand, 0) > current_timestamp,
coalesce(vip, 0) > current_timestamp,
into is_grand is_vip
from pref_users where id=_id;
但是这给了我错误:
# 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
所以我想知道该怎么办?
我真的必须
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;
或者是否有更好的方式(比如可能" 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;
但is_vip
为NULL时vip
为NULL:
# select * from pref_get_user_info('OK674418426646');
is_banned | is_grand | is_vip | rep
-----------+----------+--------+-----
f | t | |
(1 row)
当我尝试以下任何一种情况时,我会收到语法错误:
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;
我怎样才能SELECT
一次变成2个变量?
答案 0 :(得分:5)
如果你想要一个布尔值:
coalesce(grand > current_timestamp, false)
如果您需要0或1:
coalesce((grand > current_timestamp)::integer, 0)
在更新的问题中,您在选择列表和into
子句
coalesce(vip > current_timestamp, false),
into is_grand, is_vip
把它拿出来
coalesce(vip > current_timestamp, false)
into is_grand, is_vip