gpt4 book ai didi

postgresql - 在表中保留一定数量的记录

转载 作者:行者123 更新时间:2023-11-29 12:22:52 25 4
gpt4 key购买 nike

我有一个 SQL 表,其中包含玩家在纸牌游戏中收到的最后一手牌。手牌用整数表示(32 位 == 32 张牌):

create table pref_hand (
id varchar(32) references pref_users,
hand integer not NULL check (hand > 0),
stamp timestamp default current_timestamp
);

由于玩家一直在玩并且数据并不重要(只是显示在玩家资料页面上的噱头)而且我不希望我的数据库增长太快,所以我只想保持最多每个玩家 ID 10 条记录。所以我试图声明这个 PL/PgSQL 过程:

create or replace function pref_update_game(_id varchar,
_hand integer) returns void as $BODY$
begin

delete from pref_hand offset 10 where id=_id order by stamp;
insert into pref_hand (id, hand) values (_id, _hand);

end;
$BODY$ language plpgsql;

但不幸的是,这失败了:

ERROR:  syntax error at or near "offset"

因为删除不支持偏移。有人在这里有更好的主意吗?

最佳答案

类似这样的东西(未经测试!)

DELETE FROM pref_handWHERE id = _id   AND stamp in (SELECT h2.stamp                 FROM pref_hand h2                WHERE h2.id = _id                ORDER BY stamp DESC                OFFSET 10);

关于postgresql - 在表中保留一定数量的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4668787/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com