一、多表更新
语法
UPDATE table1 { [ INNER ] JOIN {LEFT | RIGHT} [ OUTER ] JOIN} table2 ON conditional_expr
SET col1 = {expr1 | DEFAULT} [, col2 = {expr2 | DEFAULT} ] .. . [
WHERE where_condition ]
实例
update readerfee t1 join readinfo t2 on t1.card_id = t2.card_id
set actual_return_date = sysdate(),
book_fee = datediff(sysdate(), return_date) * 0.2,
balance = balance - book_fee
where t1.book_id = 20151101
and t1.card_id = '210110199901012222';
二、多表查询
1、等值连接
实例:select * from emp e, dept d where e.deptno=d.deptno;
2、内连接
实例:select * from emp e inner join dept d on e.deptno = d.deptno;
3、左外链接
实例:select * from emp e left join dept d on e.deptno=d.deptno;
4、右外连接
实例:select * from emp e right join dept d on e.deptno=d.deptno;
5、自连接
定义:自连接其实就是站在不同的角度把一张表看成多张表。
实例:select e1.ename, e2.ename from emp e1, emp e2 where e1.mgr = e2.empno;
三、多表删除
语法
DELETE table1 [.* ], table [.* ] FROM table1 { [ INNER ] JOIN | {LEFT | RIGHT} [ OUTER ] JOIN} table2 ON conditional_expr [
WHERE where_condition ]
实例
delete t1 from book_category_bak t1 left join bookinfo_bak t2 on t1.category_id = t2.book_category_id
where parent_id <> 0
and book_id is null