mysql内连接查询教程
MySQL 中的内连接查询(INNER JOIN)用于结合两个或多个表中的匹配行。内连接只返回两个表中都存在的行。
内连接语法如下所示:
SELECT column_name(s)
FROM table1
INNER JOIN table2 ON table1.column_name = table2.column_name;
其中,column_name(s) 是你所选取的列名,table1 和 table2 是你所要结合的两个表名,table1.column_name 和 table2.column_name 是你所选取的连接列名。
举例来说,假设有以下两个表,一个是学生表 students,一个是课程表 courses:
学生表 students:
| id | name | age | 
|---|---|---|
| 1 | Tom | 20 | 
| 2 | Jack | 21 | 
| 3 | Mary | 19 | 
| 4 | John | 22 | 
| 5 | Jessica | 20 | 
课程表 courses:
| id | course | grade | 
|---|---|---|
| 101 | Math | A | 
| 102 | English | B | 
| 103 | Science | C | 
现在我们来查询选修了哪些课程的学生:
SELECT students.name, courses.course
FROM students
INNER JOIN courses ON students.id=courses.id;
该查询返回以下结果:
| name | course | 
|---|---|
| Tom | Math | 
| Jack | English | 
| Mary | Science | 
因为只有这三个学生选修了课程,所以只有这三行数据被返回。






 苏公网安备32021302001419号
苏公网安备32021302001419号