目录python
1.建立部门表 create table dep( id int primary key auto_increment, name varchar(16), work varchar(16) ); 2.建立员工表 create table emp( id int primary key auto_increment, name varchar(16), salary float, dep_id int ); # 插入数据 insert into dep values(1, '市场部', '销售'), (2, '教学部', '授课'), (3, '管理部', '开车'); insert into emp(name, salary, dep_id) values('egon', 3.0, 2),('yanghuhu', 2.0, 2),('sanjiang', 10.0, 1),('owen', 88888.0, 2),('liujie', 8.0, 1),('yingjie', 1.2, 0); ###################cmd图示 mysql> select * from emp; +----+----------+--------+--------+ | id | name | salary | dep_id | +----+----------+--------+--------+ | 1 | egon | 3 | 2 | | 2 | yanghuhu | 2 | 2 | | 3 | sanjiang | 10 | 1 | | 4 | owen | 88888 | 2 | | 5 | liujie | 8 | 1 | | 6 | yingjie | 1.2 | 0 | +----+----------+--------+--------+ 6 rows in set (0.00 sec) mysql> select * from dep; +----+-----------+--------+ | id | name | work | +----+-----------+--------+ | 1 | 市场部 | 销售 | | 2 | 教学部 | 授课 | | 3 | 管理部 | 开车 | +----+-----------+--------+ 3 rows in set (0.00 sec)
笛卡尔积: 集合 X{a, b} * Y{o, p, q} => Z{{a, o}, {a, p}, {a, q}, {b, o}, {b, p}, {b, q}} mysql>: select * from emp, dep; 总结:是两张表 记录的全部排列组合,数据没有利用价值
1.关键字:inner join on 2.语法:from A表 inner join B表 on A表.关联字段=B表.关联字段 select emp.id,emp.name,salary,dep.name,work from emp inner join dep on emp.dep_id = dep.id order by emp.id; #######################cmd 图解 mysql> select emp.id, emp.name salary, dep.name, work from emp join dep on emp.dep_id=dep.id order by emp.id; +----+----------+-----------+--------+ | id | salary | name | work | +----+----------+-----------+--------+ | 1 | egon | 教学部 | 授课 | | 2 | yanghuhu | 教学部 | 授课 | | 3 | sanjiang | 市场部 | 销售 | | 4 | owen | 教学部 | 授课 | | 5 | liujie | 市场部 | 销售 | +----+----------+-----------+--------+ # 总结:只保留两个表有关联的数据
1.关键字:left join on 2.语法:from 左表 left join 右表 on 左表.关联字段=右表.关联字段 select emp.id,emp.name,salary,dep.name,work from emp left join dep on emp.dep_id = dep.id order by emp.id; ###################cmd 图解 mysql> select -> emp.id,emp.name,salary,dep.name,work -> from emp left join dep on emp.dep_id = dep.id order by emp.id; +----+----------+--------+-----------+--------+ | id | name | salary | name | work | +----+----------+--------+-----------+--------+ | 1 | egon | 3 | 教学部 | 授课 | | 2 | yanghuhu | 2 | 教学部 | 授课 | | 3 | sanjiang | 10 | 市场部 | 销售 | | 4 | owen | 88888 | 教学部 | 授课 | | 5 | liujie | 8 | 市场部 | 销售 | | 6 | yingjie | 1.2 | NULL | NULL | +----+----------+--------+-----------+--------+ # 总结:保留左表的所有数据,右表有对应数据直接连表显示,没有对应关系空填充
1.关键字:right join on 2.语法:from A表 right join B表 on A表.关联字段=B表关联字段 select emp.id,emp.name,salary,dep.name,work from emp right join dep on emp.dep_id = dep.id order by emp.id; #######################cmd 图解 mysql> select -> emp.id,emp.name,salary,dep.name,work -> from emp right join dep on emp.dep_id = dep.id -> order by emp.id; +------+----------+--------+-----------+--------+ | id | name | salary | name | work | +------+----------+--------+-----------+--------+ | NULL | NULL | NULL | 管理部 | 开车 | | 1 | egon | 3 | 教学部 | 授课 | | 2 | yanghuhu | 2 | 教学部 | 授课 | | 3 | sanjiang | 10 | 市场部 | 销售 | | 4 | owen | 88888 | 教学部 | 授课 | | 5 | liujie | 8 | 市场部 | 销售 | +------+----------+--------+-----------+--------+ # 总结:保留右表的所有数据,左表有对应数据直接连表显示,没有对应关系空填充
1.left左边的是左表,右边的是右表 2.right左边的是左表,右边的是右表 3.因此咱们交换左右表的位置也是能够的 select emp.id,emp.name,salary,dep.name,work from emp right join dep on emp.dep_id = dep.id order by emp.id; mysql>: select emp.id,emp.name,salary,dep.name,work from dep left join emp on emp.dep_id = dep.id order by emp.id; # 总结:更换一下左右表的位置,相对应更换左右链接关键字,结果相同
1.就是先把两张表进行左链接,中间加一个关键字union, 而后再进行右连接 select emp.id,emp.name,salary,dep.name,work from emp left join dep on emp.dep_id = dep.id union select emp.id,emp.name,salary,dep.name,work from emp right join dep on emp.dep_id = dep.id order by id; ###############################cmd 图示 mysql> select -> emp.id,emp.name,salary,dep.name,work -> from emp left join dep on emp.dep_id = dep.id -> -> union -> -> select -> emp.id,emp.name,salary,dep.name,work -> from emp right join dep on emp.dep_id = dep.id -> -> order by id; +------+----------+--------+-----------+--------+ | id | name | salary | name | work | +------+----------+--------+-----------+--------+ | NULL | NULL | NULL | 管理部 | 开车 | | 1 | egon | 3 | 教学部 | 授课 | | 2 | yanghuhu | 2 | 教学部 | 授课 | | 3 | sanjiang | 10 | 市场部 | 销售 | | 4 | owen | 88888 | 教学部 | 授课 | | 5 | liujie | 8 | 市场部 | 销售 | | 6 | yingjie | 1.2 | NULL | NULL | +------+----------+--------+-----------+--------+ # 总结:左表右表数据都被保留,彼此有对应关系正常显示,彼此没有对应关系均空填充对方
# 建立一对一 做者与做者详情 表 create table author( id int, name varchar(64), detail_id int ); create table author_detail( id int, phone varchar(11) ); # 填充数据 insert into author values(1, 'Bob', 1), (2, 'Tom', 2), (3, 'ruakei', 0); insert into author_detail values(1, '13344556677'), (2, '14466779988'), (3, '12344332255'); # 内连 select author.id,name,phone from author join author_detail on author.detail_id = author_detail.id order by author.id; # 全连(左连加外联) select author.id,name,phone from author left join author_detail on author.detail_id = author_detail.id union select author.id,name,phone from author right join author_detail on author.detail_id = author_detail.id order by id;
# 在一对一基础上,创建 做者与书 的多对多关系关系 # 利用以前的做者表 create table author( id int, name varchar(64), detail_id int ); insert into author values(1, 'Bob', 1), (2, 'Tom', 2), (3, 'ruakei', 0); # 建立新的书表 create table book( id int, name varchar(64), price decimal(5,2) ); insert into book values(1, 'python', 3.66), (2, 'Linux', 2.66), (3, 'Go', 4.66); # 建立 做者与书 的关系表 create table author_book( id int, author_id int, book_id int ); # 数据:author-book:1-1,2 2-2,3 3-1,3 insert into author_book values(1,1,1),(2,1,2),(3,2,2),(4,2,3),(5,3,1),(6,3,3); # 将有关联的表一一创建链接,查询因此本身所需字段 select book.name, book.price, author.name from book join author_book on book.id = author_book.book_id join author on author_book.author_id = author.id; select book.name,book.price,author.name,author_detail.phone from book join author_book on book.id =author_book.book_id join author on author_book.author_id = author.id left join author_detail on author.detail_id = author_detail.id