MySQL之三张表关联

创建三张表

1、学生表

mysql> create table students(
 sid int primary key auto_increment,
 sname varchar(100) not null,
 age int,
 address varchar(100),
 courseid int,
 constraint fk_stu_cid foreign key(courseid)
 references course(cid)
 );
mysql> insert into students(sname,age,address,courseid) values('小海子',23,'北京',1003);
mysql> insert into students(sname,age,address,courseid) values('小沈阳',45,'沈阳',1003);
mysql> insert into students(sname,age,address,courseid) values('刘阳',25,'山东',1002);
mysql> insert into students(sname,age,address,courseid) values('甘能',22,'广东',1002);
mysql> select * from students;
+-----+--------+-----+---------+----------+
| sid | sname  | age | address | courseid |
+-----+--------+-----+---------+----------+
|   1 | 小海子 |  23 | 北京    |     1003 |
|   2 | 小沈阳 |  45 | 沈阳    |     1003 |
|   3 | 刘阳   |  25 | 山东    |     1002 |
|   4 | 甘能   |  22 | 广东    |     1002 |
+-----+--------+-----+---------+----------+

 

2、老师表

mysql> create table teacher(
    -> tid int(5) primary key auto_increment,
    -> tname varchar(100) not null,
    -> age int(4),
    -> address varchar(100),
    -> courseid int
    -> )engine=innodb auto_increment=101;
mysql> insert into teacher(tname,age,address) values('马云',50,'杭州');
mysql> insert into teacher(tname,age,address) values('赵本山',52,'沈阳');
mysql> insert into teacher(tname,age,address) values('刘强东',45,'北京');
mysql> select* from teacher;
+-----+--------+-----+---------+----------+
| tid | tname  | age | address | courseid |
+-----+--------+-----+---------+----------+
| 101 | 马云   |  50 | 杭州    | NULL     |
| 102 | 赵本山 |  52 | 沈阳    | NULL     |
| 103 | 刘强东 |  45 | 北京    | NULL     |
+-----+--------+-----+---------+----------+

3、课程表

mysql> create table course(
    -> cid int primary key auto_increment,
    -> cname varchar(100) not null,
    -> xuefen int,
    -> tid int,
    -> constraint fk_course_tid foreign key(tid)
    -> references teacher(tid)
    -> )engine=innodb auto_increment = 1001;
mysql> insert into course(cname,xuefen,tid) values('C++',3,'101');
mysql> insert into course(cname,xuefen,tid) values('java',5,'101');
mysql> insert into course(cname,xuefen,tid) values('相声表演',2,'102');
mysql> insert into course(cname,xuefen,tid) values('电子商务',3,'103');
mysql> select * from course;
+------+----------+--------+-----+
| cid  | cname    | xuefen | tid |
+------+----------+--------+-----+
| 1001 | C++      |      3 | 101 |
| 1002 | java     |      5 | 101 |
| 1003 | 相声表演 |      2 | 102 |
| 1004 | 电子商务 |      3 | 103 |
+------+----------+--------+-----+

 学生修了哪些课程

mysql> select s.sname,c.cid,c.cname
    -> from students s left join course c
    -> on s.courseid = c.cid;
+--------+------+----------+
| sname  | cid  | cname    |
+--------+------+----------+
| 刘阳   | 1002 | java     |
| 甘能   | 1002 | java     |
| 小海子 | 1003 | 相声表演 |
| 小沈阳 | 1003 | 相声表演 |
+--------+------+----------+

学生修的课程有哪些老师教

mysql> select s.sname,c.cid,c.cname,t.tname
    -> from students s,course c,teacher t
    -> where s.courseid = c.cid and c.tid = t.tid;
+--------+------+----------+--------+
| sname  | cid  | cname    | tname  |
+--------+------+----------+--------+
| 刘阳   | 1002 | java     | 马云   |
| 甘能   | 1002 | java     | 马云   |
| 小海子 | 1003 | 相声表演 | 赵本山 |
| 小沈阳 | 1003 | 相声表演 | 赵本山 |
+--------+------+----------+--------+

或者

mysql> select s.sname,c.cid,c.cname,t.tname
    ->  from students s inner join course c inner join teacher t
    -> on  s.courseid = c.cid and c.tid = t.tid;

+--------+------+----------+--------+
| sname  | cid  | cname    | tname  |
+--------+------+----------+--------+
| 刘阳   | 1002 | java     | 马云   |
| 甘能   | 1002 | java     | 马云   |
| 小海子 | 1003 | 相声表演 | 赵本山 |
| 小沈阳 | 1003 | 相声表演 | 赵本山 |
+--------+------+----------+--------+

其他关联不行。left join ,right join

 2018年1月17日01:07:40

posted on 2018-01-17 01:08  墨~影  阅读(14169)  评论(0编辑  收藏  举报

导航