子查询 步骤: 1、从学生表中查询出学生成绩等于100的学生的所有信息; 2、从学生表中查询出姓名,年龄信息;再从成绩表中查询出科目,成绩信息;最后将两张表中的信息合并到一张表中显示;要求去除重复记录用一个sql语句来完成。
时间: 2024-03-25 08:35:39 浏览: 32
1. 查询成绩等于100的学生的所有信息:
```
SELECT *
FROM student
WHERE student_id IN (SELECT student_id FROM score WHERE score = 100);
```
2. 查询学生、成绩表的信息并合并去重:
```
SELECT DISTINCT student.name, student.age, score.subject, score.score
FROM student
JOIN score ON student.student_id = score.student_id;
```
将两个查询合并:
```
SELECT DISTINCT student.name, student.age, score.subject, score.score
FROM student
JOIN score ON student.student_id = score.student_id
WHERE student.student_id IN (SELECT student_id FROM score WHERE score = 100);
```
注意,这里使用了IN子查询,可以用EXISTS子查询代替,效率会更高。
阅读全文