sql 数据库 一对多查询

2025-02-24 12:06:37
推荐回答(2个)
回答1:

你怎么俩table1,后一个叫table2吧

 

执行以下语句

select t1.考试学号,t1.科目,t2.考试成绩
from
(select a.科目,a.科目ID,b.考试学号
from
(select 科目,科目ID from table1) as a,
(select distinct 考试学号 from table2) as b) as t1 left join
table2 as t2 on t1.科目ID=t2.科目ID and t1.考试学号=t2.考试学号

 

结果截图

 

下边还有几条没显示全,凑合看吧

回答2:

select t1.考试学号,t2.科目名,(select t3.成绩 from tab_student t3 where t3.学号= t1.学号 and t2.科目ID= t3.科目ID)
from (select distinct 考试学号 from tab_student) t1, tab_科目 t2