我想創建一個upvote函式,僅當uid引數不包含在already_voted_uid陣列中時才會增加一個數字,并且如果滿足條件,還會將該 uid 添加到該陣列中。這是我嘗試的偽代碼:
create function upvote (cid text, uid uuid)
returns void as $$
begin
update submissions
set number_of_votes = number_of_votes 1
where cid = cid
and already_voted_uids does not includes uid <-- this is pseudo code
set already_voted_uids = already_voted_uid.push(uid) <-- this is pseudo code
end;
$$ language plpgsql security definer;
already_voted_uids 是一組uid
uj5u.com熱心網友回復:
您可以使用 <> ALL()
create function upvote (cid_to_upvote text, uid text)
returns void as $$
begin
update painting_submissions
set number_of_votes = number_of_votes 1,
already_voted_uid = already_voted_uid||uid
where cid = cid_to_upvote
and uid <> all(already_voted_uid);
end;
$$ language plpgsql security definer;
uj5u.com熱心網友回復:
所以我設法找到了一個可行的解決方案:
create function upvote (cid_to_upvote text, uid text)
returns void as $$
begin
update painting_submissions
set number_of_votes = number_of_votes 1, already_voted_uid = array_append(already_voted_uid, uid)
where cid = cid_to_upvote
and NOT EXISTS (
SELECT 1
FROM unnest(already_voted_uid) AS voted
WHERE voted LIKE uid
);
end;
$$ language plpgsql security definer;
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/317112.html
標籤:PostgreSQL
