cron
, pgAgent
또는 유사한 작업 스케줄러를 사용하여 이와 같은 작업을 수행 할 수 있습니다.
하지만 이렇게 할 필요는 없습니다. 그냥 같은 테이블을 사용 : 선거는 votes=0
과 모든 후보에게 election_candidates
행을 작성 시작
create table election_candidates (
election_id int not null references elections(election_id),
user_id int not null references users(user_id),
votes int not null default 0
);
create index election_candidates_election_id_votes_idx
on election_candidates(election_id, votes);
. 투표를받을 때 update election_candidates set votes=votes+1 where election_id=? and user_id=?
을 사용하기 만하면됩니다. 투표를 기록하려면 다른 표에서 투표를 수행하고 트리거를 사용하여이 표를 업데이트하십시오.
방금 사용 승자를 확인해야하는 경우 :
with max_votes as (
select election_id, max(votes) as max_vote from election_candidates
where election_id=?
group by election_id
)
select user_id from election_candidates
natural inner join max_votes
where votes=max_vote;
을하지만 더 이상의 후보가 투표의 같은 번호를받을 때 하나 이상의 승자가있을 수 있음을 기억하십시오.
[pgAgent] (http://www.pgadmin.org/download/pagagent.php)를 확인할 수 있습니다. – zero323
코드 – Phate
으로 대답하고 싶습니다. "datetime에 도달했을 때 각성시키는 트리거를 만들 수 있습니까?"라는 질문에 대한 대답은 "아니오"입니다. 외부 프로세스를 사용하여 작업을 트리거해야합니다. – bma