DELETE删除记录以及嵌套查询

create database testjoin character set utf8mb4;

create table ta(id int primary key auto_increment not null, name char(255));

create table tb(id int primary key auto_increment not null, size char(255));

create table tc(id int primary key auto_increment not null, ta_id int, tb_id int, FOREIGN KEY (ta_id) REFERENCES ta(id), FOREIGN KEY (tb_id) REFERENCES tb(id));

insert into ta values(NULL, '键盘');
insert into ta values(NULL, '鼠标');
insert into ta values(NULL, '显示器');
insert into ta values(NULL, '主机');

insert into tb values(NULL, '大');
insert into tb values(NULL, '中');
insert into tb values(NULL, '小');

insert into tc values(NULL, 1, 1);
insert into tc values(NULL, 1, 2);
insert into tc values(NULL, 1, 3);
insert into tc values(NULL, 2, 1);
insert into tc values(NULL, 2, 2);
insert into tc values(NULL, 3, 1);

select ta.name, tb.size from ta join tc on ta.id=tc.ta_id join tb on tc.tb_id=tb.id;

delete from tc where tc.ta_id=(select ta.id from ta where name='键盘');

drop database testjoin;
文章目录