利用三张表分别用连接查询和子查询两种方法, 查询课程名称为"C语言程序设计”,且分数大于等于80的学生姓名和分数。
时间: 2024-10-11 11:13:41 浏览: 25
php+mysqli批量查询多张表数据的方法
首先,假设我们有三个相关的数据库表:
1. **学生信息表** (Students),包含字段:StudentID, Name, CourseID
2. **课程信息表** (Courses),包含字段:CourseID, CourseName
3. **成绩表** (Grades),包含字段:StudentID, CourseID, Score
使用连接查询(JOIN)的方法:
```sql
SELECT Students.Name, Grades.Score
FROM Students
INNER JOIN Courses ON Students.CourseID = Courses.CourseID
INNER JOIN Grades ON Students.StudentID = Grades.StudentID
WHERE Courses.CourseName = 'C语言程序设计'
AND Grades.Score >= 80;
```
使用子查询(Subquery)的方法:
```sql
SELECT s.Name, g.Score
FROM (
SELECT StudentID, CourseName
FROM Courses
WHERE CourseName = 'C语言程序设计'
) AS c
INNER JOIN Students AS s ON s.CourseID = c.StudentID
INNER JOIN Grades AS g ON s.StudentID = g.StudentID
WHERE g.Score >= 80;
```
阅读全文