我正在开发一个民意调查应用程序。SQL 模式是:
polls -> * options (poll_id) -> * answers (option_id)
或者:“投票有很多选项,选项可以有答案(又名投票)”
每次投票我只能允许每个用户投一票。这是合并子句(显然不起作用):
merge into answers
using (select count(*)
from answers, options, polls
where answers.option_id = options.id
and options.poll_id = polls.id
and polls.id = {poll_id}
and answers.owner_id = {owner_id}) votes
on (votes = 0)
when matched then
insert into answers values (NULL, {option_id}, {owner_id}, NOW())