Mysql sql中的对称对
给出了学生、数学、科学的分数Mysql sql中的对称对,mysql,sql,join,select,Mysql,Sql,Join,Select,给出了学生、数学、科学的分数 student_id (Primary Key) | smallint score | float (5,2) 学生 student_id (Primary Key) | smallint student_name | varchar(30) 数学成绩 student_id (Primary Key) | smallint score | floa
student_id (Primary Key) | smallint
score | float (5,2)
学生
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
一个适当设计的模式将有一个单独的表,用于学生、科目和分数。然后,第一步是模拟该表,可以如下所示:
SELECT student_id
, 'mathematics' subject
, score
FROM mathematics_marks
UNION
SELECT student_id
, 'science'
, score
FROM science_marks
如需更多帮助,请参阅:如果文章附有样本数据和预期输出,那么回答起来总是很容易的。你在找这样的东西吗
Select student.student_id
,maths.score maths_socer
,sci.score science_score
from
(
Select 1 student_id union all
Select 2 union all
Select 3
) student
Join
(
Select 1 student_id, 20 score union all
Select 2, 30 union all
Select 3, 40
) maths
on maths.student_id=student.student_id
Join
(
Select 1 student_id , 30 score union all
Select 2,20 union all
Select 3,40
) sci on sci.student_id !=student.student_id
Where sci.score = maths.score
你就不会有单独的数学和科学表格。在这种情况下,float是不合适的。那应该是十进制的。我先加入了这两个表,那是你的第一个错误。那将是一个联合体。哦,好的,我也会检查一下,但是join为我工作,谢谢你的帮助。从现在开始请澄清。他们(似乎)想要(s1)行,其中存在s2,m,s[m(s1,m)&s(s1,s)&m(s2,s)&s(s2,m)&s(s2,m)&s1s2]。不需要合并,因为所需的学生必须出现在两个表中,而不是两个表中。如果标记行然后进行联合,那么仍然必须使用重新创建原始表限制的限制进行联接&所有所需的学生都出现在联接的(对称)限制的两列中。所以这对工会没有好处。