Sql 如何在Oracle中获取不匹配的行
有人能帮我从Oracle中的两个表中提取不匹配的行吗 表:名称 表:课程 输出 我需要将这些行显示为Sql 如何在Oracle中获取不匹配的行,sql,oracle,Sql,Oracle,有人能帮我从Oracle中的两个表中提取不匹配的行吗 表:名称 表:课程 输出 我需要将这些行显示为 CLASS ID STUD_NAME_FROM_NAME_TABLE STUD_NAME_FROM_COURSE_TABLE --------------------------------------------------------------------- S001 PETER KEITH
CLASS ID STUD_NAME_FROM_NAME_TABLE STUD_NAME_FROM_COURSE_TABLE
---------------------------------------------------------------------
S001 PETER KEITH
我已使用Oracle联接获取不匹配的名称:
SELECT *
FROM Names, Course
WHERE Names.Class_id=Course.Course_id
AND Names.Stud_name<>Course.Stud_name
选择*
当然是名字
其中Names.Class\u id=Course.Course\u id
和名称。Stud_name课程。Stud_name
此查询返回重复的行。当您请求不匹配的行时,我假设您想要的行存在于名称中,但不存在于课程中
如果这是你要找的案子
select * from names
where (class_id, stud_name ) not in
(select course_id, stud_name from course);
您的查询返回了重复的行,因为对于names
中的每一行,它选择了course
中满足where
条件的所有行
因此,对于S001行,names
中的PETER
发现S001、JAMES
和S001、KEITH
符合该条件,因此该行被“返回”两次
编辑由于不清楚stud\u name
是主键还是唯一键(乍一看我认为不是),您可能需要一个
select * from names
where not exists (
select 1 from course where
names.class_id = course.course_id and
names.stud_name <> course.stud_name
)
获取名称
表中不匹配的行
SELECT * FROM Names
WHERE
NOT EXISTS
(SELECT 'x' from Course
WHERE
Names.Class_id = Course.Course_id AND
Names.Stud_name = Course.Stud_name)
获取名称
和课程
中不匹配的行
SELECT Names.Class_id,Names.Stud_name,C1.Stud_name
FROM Names , Course C1
WHERE Names.Class_id = C1.Course_id AND
NOT EXISTS
(SELECT 'x' from Course C2
WHERE
Names.Class_id = C2.Course_id AND
Names.Stud_name = C2.Stud_name);
如果您坚持加入,您可以使用以下选项:
SELECT *
FROM Names
FULL OUTER JOIN Course ON Names.Class_id=Course.Course_id
AND Names.Stud_name = Course.Stud_name
WHERE Names.Stud_name IS NULL or Course.Stud_name IS NULL
嗯,我不确定我是否正确理解了你的要求。我想您需要一个所有ID的列表,其中班级表和课程表中的学生列表不同。然后,您要显示id和在课堂上但不在课堂上的学生,以及在课堂上但不在课堂上的学生
要做到这一点,您需要完全连接表。这就给了你们上课和上课的学生,上课和不上课的学生,上课和不上课的学生。如果class_id或course_id为空,则过滤结果,以获取课程或班级中缺少的学生。最后按id分组,列出学生名单
select coalesce(class.class_id, course.course_id) as id
, listagg(class.stud_name, ',') within group (order by class.stud_name) as missing_in_course
, listagg(course.stud_name, ',') within group (order by course.stud_name) as missing_in_class
from class
full outer join course
on (class.class_id = course.course_id and class.stud_name = course.stud_name)
where class.class_id is null or course.course_id is null
group by coalesce(class.class_id, course.course_id);
下面是SQL FIDLE,展示了它的工作原理:
编辑:在Oracle 9i中没有listagg。您可以改用非正式函数wm_concat:
select coalesce(class.class_id, course.course_id) as id
, wm_concat(class.stud_name) as missing_in_course
, wm_concat(course.stud_name) as missing_in_class
from class
full outer join course
on (class.class_id = course.course_id and class.stud_name = course.stud_name)
where class.class_id is null or course.course_id is null
group by coalesce(class.class_id, course.course_id);
希望对你有帮助
with not_in_class as
(select a.*
from Names a
where not exists ( select 'x'
from course b
where b.Course_id = a.class_id
and a.Stud_name = b.Stud_name)),
not_in_course as
(select b.*
from course b
where not exists ( select 'x'
from Names a
where b.Course_id = a.class_id
and a.Stud_name = b.Stud_name))
select x.class_id,
x.Stud_name NOT_IN_CLASS,
y.stud_name NOT_IN_COURSE
from not_in_class x, not_in_course y
where x.class_id = y.course_id
输出
| CLASS_ID | NOT_IN_CLASS | NOT_IN_COURSE |
|----------|--------------|---------------|
| S001 | PETER | KEITH |
唯一的问题是,如果给定id的两个表中存在多个不匹配项,则特定id的单个不匹配项有效。如果同一id存在多个不匹配项,则需要返工。只需遵循链接即可。愿你得到这份工作answer@Rams如何从S001的名称表中确认Peter或James是正确的?不清楚您想要什么。这从数据模型开始。为什么一个班级和一个课程都使用相同的id?这些表是否应该显示相同的数据,而您正在寻找差异?班上所有没有上相应课程的学生?所有不在相应班级的学生呢?除了列出错误的学生,你还想列出每个班级/课程id的错误学生吗?是这样吗?如果查询返回重复的行,请在查询中使用distinct。谢谢,我正在尝试使用联接,是否有可能实现任何联接技术而不是运算符中的联接。我的out应该看起来像我在使用oracle 9i,因此您能提供一个解决方案吗?谢谢请在oracle 9i中提供一个替代解决方案,感谢Ora9i没有官方的字符串聚合函数。但是,您可以使用未记录的wm_concat。(否则你必须自己编写一个字符串聚合。)我已经编辑了我的答案。似乎Wm Concat在Oracle 9i中也不支持,是否有其他替代方案对不起,我不知道。然后请看这里:嗨,上面的查询显示错误,它的给出错误,我不确定出错了什么对不起,我错过了子句:-P再次更新了查询。嗨,上面的查询返回重复的行,它没有按照我的要求给出准确的结果
select coalesce(class.class_id, course.course_id) as id
, wm_concat(class.stud_name) as missing_in_course
, wm_concat(course.stud_name) as missing_in_class
from class
full outer join course
on (class.class_id = course.course_id and class.stud_name = course.stud_name)
where class.class_id is null or course.course_id is null
group by coalesce(class.class_id, course.course_id);
with not_in_class as
(select a.*
from Names a
where not exists ( select 'x'
from course b
where b.Course_id = a.class_id
and a.Stud_name = b.Stud_name)),
not_in_course as
(select b.*
from course b
where not exists ( select 'x'
from Names a
where b.Course_id = a.class_id
and a.Stud_name = b.Stud_name))
select x.class_id,
x.Stud_name NOT_IN_CLASS,
y.stud_name NOT_IN_COURSE
from not_in_class x, not_in_course y
where x.class_id = y.course_id
| CLASS_ID | NOT_IN_CLASS | NOT_IN_COURSE |
|----------|--------------|---------------|
| S001 | PETER | KEITH |