我有以下查询,
(select student_id from student where std = 8)
intersect
(select student_id from student where std = 7)
intersect
(select student_id from student where std = 9)
union
(select student_id from student where std = 10)
现在,我不想使用 intersect
,而不是我要使用 inner join
所以我将查询转换为以下
select * from
(
(select student_id from student where std = 8)
as res1 inner join
(select student_id from student where std = 9)
as res2 on res1.student_id = res2.student_id inner join
(select student_id from student where std = 7)
as res3 on res2.student_id = res3.student_id
)
union
(select student_id from student where std = 10)
在这种情况下,我有以下错误:
使用联合,相交或运算符除外的所有查询必须 在其目标列表中具有相等数量的表达式。
,因为您加入了,并且使用 SELECT *
,因此查询以格式返回记录
STUDENT_ID | STUDENT_ID
,第二个查询仅返回1列。
使用此:
select t.student_id
from student t
inner join student s
ON(t.student_id = s.student_id)
WHERE s.std = 9 and t.std = 8
UNION
select student_id from student where std = 10
列之间可能存在差异。当您使用联合时,所选列必须相同,您在第一个选择中有 *在最后(在联合下)中的student_id。