【发布时间】:2020-04-10 20:29:42
【问题描述】:
给定表格 student、maths_marks、science_marks
学生
student_id (Primary Key) | smallint
student_name | varchar(30)
数学标记
student_id (Primary Key) | smallint
score | float (5,2)
科学标记
student_id (Primary Key) | smallint
score | float (5,2)
如果该学生在科学中获得的分数与其他学生在数学中获得的分数相等,并且在数学中获得的分数与该学生获得的分数相同,则该学生被称为对称对的一部分其他理科学生。
编写 SQL 查询以打印属于对称对的学生的姓名。按学生姓名的顺序排列输出。
with cte as (
select m.student_id, m.score as math_score, s.score as science_score
from mathematics_marks m inner join science_marks s
on s.student_id = m.student_id
)
select s1.student_name
from cte c1 inner join cte c2
on c2.student_id > c1.student_id and c2.math_score = c1.science_score and c1.math_score = c2.science_score
inner join student s1 on s1.student_id = c1.student_id and s1.student_id = c2.student_id
需要将这对放在两个不同的行中吗?
编辑: 回答这个问题
with cte as (
select m.student_id, m.score as math_score, s.score as science_score
from mathematics_marks m inner join science_marks s
on s.student_id = m.student_id
),
T as (select c1.student_id, c1.math_score, c1.science_score, s.student_name as name
from cte c1 inner join student s
on c1.student_id = s.student_id )
select t1.name from T t1 inner join T t2
on t1.math_score = t2.science_score and
t1.science_score = t2.math_score
order by t1.name
【问题讨论】:
-
您不会有单独的数学和科学表格。在这种情况下,float 是不合适的。应该是 DECIMAL。
-
@Strawberry 我先加入了那两张桌子
-
那是你当时的第一个错误。那将是一个 UNION。
-
哦,好的,我也会检查一下,但加入对我有用,谢谢你的帮助
-
从这个时间开始尝试清楚。