语法1:
DELETE tab1e1[.*],tab1e2[.*]
FROM tab1e1
INNER JOIN table2
ON condition_expr
WHERE where_conditions
语法2:
DELETE tab1e1[.*],tab1e2[.*]
FROM tab1e1
LEFT JOIN table2
ON condition_expr
WHERE where_conditions
语法3:
DELETE tab1e1[.*],tab1e2[.*]
FROM tab1e1
RIGHT JOIN table2
ON condition_expr
WHERE where_conditions
复制表格:
-复制已存在表bookcategory命名为bookcategory_bak
create table bookcategory_bak
as
select * from bookcategory;
-复制已存在表bookinfo命名为bookinfo_bak
create table bookinfo_bak
as
select * from bookinfo;
-查看复制好的两个表
select * from bookcategory_bak;
select * from bookinfo_bak;
案例1:
需求:由于业务需求,需要删除图书类别表中在图书信息表中没有图书记录的类别。
select book_id,book_name,category from bookcategory_bak t1
left join bookinfo_bak t2
on t1.category_id = t2.book_category_id
where parent_id<>0;
delete t1 from bookcategory_bak t1
left join bookinfo_bak t2
on t1.category_id = t2.book_category_id
where parent_id<>0 and book_id is null;
select * from bookcategory_bak;
案例2:
需求:删除图书类别表的编程语言的类别,以及图书信息表中关于编程语言的图书记录。
select book_id,book_name,category_id,category from bookcategory_bak t1
inner join bookinfo_bak t2
on t1.category_id = t2.book_category_id;
delete t1,t2 from bookcategory_bak t1
inner join bookinfo_bak t2
on t1.category_id = t2.book_category_id
where t1.category_id = 3;