#一下两张表:
mysql> select * from student;
+----------+--------+------+------------+-------+-----------------+
| Sno      | Sname  | Ssex | Sbrithday  | Sdept | Speciality      |
+----------+--------+------+------------+-------+-----------------+
| 20180101 | 李勇   | 男   | 2000-01-12 | CS    | 计算机应用      |
| 20180201 | 刘晨   | 女   | 2001-06-04 | IS    | 电子商务        |
| 20180202 | 张立   | 男   | 2001-08-25 | IS    | 电子商务        |
| 20180301 | 王敏   | 女   | 2002-12-23 | MA    | 数学            |
+----------+--------+------+------------+-------+-----------------+
4 rows in set (0.00 sec)

mysql> select * from sc;
+----------+-----+--------+
| Sno      | Cno | Degree |
+----------+-----+--------+
| 20180101 | C01 |     92 |
| 20180101 | C02 |     85 |
| 20180101 | C03 |     88 |
| 20180201 | C02 |     90 |
| 20180201 | C03 |     80 |
| 20180301 | C01 |     89 |
| 20180301 | C02 |     87 |
| 20180202 | C01 |     87 |
+----------+-----+--------+
8 rows in set (0.00 sec)
-- 查询利用的选修的课名和成绩   --->应该想到是两张表进行连接
-- 有以下三种方式:
-- 第一种方式 where方式
mysql> select Sname,Cno,Degree from student,sc where student.sno=sc.sno and sname='李勇';
+--------+-----+--------+
| Sname  | Cno | Degree |
+--------+-----+--------+
| 李勇   | C01 |     92 |
| 李勇   | C02 |     85 |
| 李勇   | C03 |     88 |
+--------+-----+--------+
3 rows in set (0.00 sec)

-- 第二种方式是利用嵌套来做:
mysql> select Cno,Degree from sc where sc.sno=(select sno from student where Sname='李勇');
+-----+--------+
| Cno | Degree |
+-----+--------+
| C01 |     92 |
| C02 |     85 |
| C03 |     88 |
+-----+--------+
3 rows in set (0.00 sec)

-- 第三种采用等值连接inner join    on来操作
mysql> select Sname,Cno,Degree from student inner join sc on student.sno=sc.sno where Sname='李勇';
+--------+-----+--------+
| Sname  | Cno | Degree |
+--------+-----+--------+
| 李勇   | C01 |     92 |
| 李勇   | C02 |     85 |
| 李勇   | C03 |     88 |
+--------+-----+--------+
3 rows in set (0.00 sec)

[源于MySQL等值连接](MySQL 连接的使用 | 菜鸟教程 (runoob))

#当我选择student和sc两张表中的*使用where表达式的时候会出两列Sno:
mysql> select * from student,sc where student.sno=sc.sno;
+----------+--------+------+------------+-------+-----------------+----------+-----+--------+
| Sno      | Sname  | Ssex | Sbrithday  | Sdept | Speciality      | Sno      | Cno | Degree |
+----------+--------+------+------------+-------+-----------------+----------+-----+--------+
| 20180101 | 李勇   | 男   | 2000-01-12 | CS    | 计算机应用      | 20180101 | C01 |     92 |
| 20180101 | 李勇   | 男   | 2000-01-12 | CS    | 计算机应用      | 20180101 | C02 |     85 |
| 20180101 | 李勇   | 男   | 2000-01-12 | CS    | 计算机应用      | 20180101 | C03 |     88 |
| 20180201 | 刘晨   | 女   | 2001-06-04 | IS    | 电子商务        | 20180201 | C02 |     90 |
| 20180201 | 刘晨   | 女   | 2001-06-04 | IS    | 电子商务        | 20180201 | C03 |     80 |
| 20180202 | 张立   | 男   | 2001-08-25 | IS    | 电子商务        | 20180202 | C01 |     87 |
| 20180301 | 王敏   | 女   | 2002-12-23 | MA    | 数学            | 20180301 | C01 |     89 |
| 20180301 | 王敏   | 女   | 2002-12-23 | MA    | 数学            | 20180301 | C02 |     87 |
+----------+--------+------+------------+-------+-----------------+----------+-----+--------+
8 rows in set (0.00 sec)

#其中where表达式的作用就是将两个表进行等值连接,但是出现了两列Sno,怎么消除?
mysql> select * from student inner join sc using(sno);
+----------+--------+------+------------+-------+-----------------+-----+--------+
| Sno      | Sname  | Ssex | Sbrithday  | Sdept | Speciality      | Cno | Degree |
+----------+--------+------+------------+-------+-----------------+-----+--------+
| 20180101 | 李勇   | 男   | 2000-01-12 | CS    | 计算机应用      | C01 |     92 |
| 20180101 | 李勇   | 男   | 2000-01-12 | CS    | 计算机应用      | C02 |     85 |
| 20180101 | 李勇   | 男   | 2000-01-12 | CS    | 计算机应用      | C03 |     88 |
| 20180201 | 刘晨   | 女   | 2001-06-04 | IS    | 电子商务        | C02 |     90 |
| 20180201 | 刘晨   | 女   | 2001-06-04 | IS    | 电子商务        | C03 |     80 |
| 20180202 | 张立   | 男   | 2001-08-25 | IS    | 电子商务        | C01 |     87 |
| 20180301 | 王敏   | 女   | 2002-12-23 | MA    | 数学            | C01 |     89 |
| 20180301 | 王敏   | 女   | 2002-12-23 | MA    | 数学            | C02 |     87 |
+----------+--------+------+------------+-------+-----------------+-----+--------+
8 rows in set (0.00 sec)

#因为要消除的是相同的Sno这一列的属性,因此使用using()就是相当于student.sno=sc.sno,并且还起到了消除重复列的作用

更多推荐

【数据库】MySQL进行两表连接的时候如何消除重复列?