模式:
drop table if exists people;
create table people
( id int primary key,
name varchar(100) not null,
gender char(1) not null
);
insert people (id,name,gender) values
(1,'Kathy','f'),(2,'John','m'),(3,'Paul','m'),(4,'Kim','m');
drop table if exists pets;
create table pets
( id int auto_increment primary key,
ownerId int not null,
name varchar(100) not null,
color varchar(100) not null,
foreign key `fk_pets_2_people` (ownerId) references people(id)
);
insert pets(ownerId,name,color) values
(1,'Rover','beige'),(2,'Bubbles','purple'),(3,'Spot','black and white'),
(1,'Rover2','white');
查詢:
DELETE p1
FROM people p1
JOIN pets p2
ON p2.ownerId = p1.id
AND p1.name = 'Paul';
-- error 1451: FK violation (you would orphan poor Spot, left to feed himself)
-- and your constraint said not to do that
DELETE p2
FROM people p1
JOIN pets p2
ON p2.ownerId = p1.id
AND p1.name = 'Paul';
-- Spot is now gone
select * from pets;
+----+---------+---------+--------+
| id | ownerId | name | color |
+----+---------+---------+--------+
| 1 | 1 | Rover | beige |
| 2 | 2 | Bubbles | purple |
| 4 | 1 | Rover2 | white |
+----+---------+---------+--------+
刷新數據,然後。
DELETE p1,p2
FROM people p1
JOIN pets p2
ON p2.ownerId = p1.id
AND p1.name = 'Paul';
-- 2 rows deleted Spot is now gone
select * from people;
+----+-------+--------+
| id | name | gender |
+----+-------+--------+
| 1 | Kathy | f |
| 2 | John | m |
| 4 | Kim | m |
+----+-------+--------+
select * from pets;
+----+---------+---------+--------+
| id | ownerId | name | color |
+----+---------+---------+--------+
| 1 | 1 | Rover | beige |
| 2 | 2 | Bubbles | purple |
| 4 | 1 | Rover2 | white |
+----+---------+---------+--------+
正如問題中的註釋示例中所述,請查看您的特定情況下的級聯刪除(如果相關)。
您的查詢(這是你的意思辦?)
DELETE mi
FROM modifier_categories mc
INNER JOIN modifier_items mi
ON mi.cat_parent_id=mc.cat_id
WHERE mc.menu_item_id = 28560;
如果你想這樣做沒有任何意義,我 – Drew
[級聯刪除](http://stackoverflow.com/a/322984 05),然後通過創建這些關係來完成它們。看到這個參考,它的後半部分。你沒有顯示你的模式(爲每個xyz使用'show create table xyz')。您不會告訴您的查詢在刪除第二個表中的行時做任何事情。 – Drew
你目前正在做的是一個類比。你有一張「人」桌子和一張「寵物」桌子。在你的40人中,可能會有一個叫保羅的人。現在,將人們加入寵物,完成後刪除Paul。 – Drew