在where子句中是唯一的



我一直试图用oracle11g运行这段代码,但一直出现一个错误,即"ORA-00936:缺少表达式"。我认为问题出在unique子句上,但我不知道为什么。有什么想法吗?

select id , name , dept_name , salary
from instructor 
where unique (select teaches.id
from teaches 
where instructor.id = teaches.id and year = 2009 );

我想你想要

select distinct id , name , dept_name , salary

而你在哪里应该有一个"入"声明

大概你想要的是2009年只教过一次的讲师。

如果是这样,你可以做:

select id, name, dept_name, salary
from instructor i
where (select count(*) from teaches t where i.id = t.instructor_id and t.year = 2009) = 1;

我希望teaches表有一个instructor_id列,而不是与instructor(id)相关的id列。

另一个选项使用聚合:

select i.id, i.name, i.dept_name, i.salary
from instructor i
inner join teaches t on t.instructor_id = i.id and t.year = 2009
group by i.id, i.name, i.dept_name, i.salary
having count(*) = 1

解释您想要做什么以及示例数据肯定会有所帮助;我们只知道你写了一个无效的查询。

与此同时,还有一个幸运的猜测:

select i.id, i.name, i.dept_name, i.salary
from instructor i join teaches t on t.id = i.id
where t.year = 2009

"我需要尝试使用唯一子句">

UNIQUE不是运算符。你能做的是:

select id , name , dept_name , salary
from instructor 
where instructor.id in  (select unique teaches.id
from teaches 
where   year = 2009 
group by teaches.id having count(*) = 1);

然而,UNIQUE是多余的,因为它具有相同的效果:

select id , name , dept_name , salary
from instructor 
where instructor.id in  (select teaches.id
from teaches 
where   year = 2009 
group by teaches.id having count(*) = 1 );

如果您对我们给出的答案不满意,您需要发布教程中的链接或引用。因为您似乎正在尝试做一些Oracle SQL语法不支持的事情。

最新更新