【发布时间】:2012-07-16 21:45:47
【问题描述】:
我正在开发一个投票应用程序。 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())
【问题讨论】:
-
now()不是有效的 Oracle 函数。 -
是的,这个查询来自我们使用 H2 的开发服务器。我需要它与 11g 一起使用,如果没有标准 SQL,我可以在 dev 上填充该方法或任何东西
-
这些主键上有什么样的索引?
-
也许您应该使用带有 NOT IN 的子查询?