2016-07-17 44 views
0

我想用inner join刪除2個表格。我有一個modifier_category表,這是父表和modifier_items表是孩子。爲什麼我的刪除內部連接不工作?

它們涉及的方法是存儲在所述cat_idmodifier_categories表主ID在modifier_items

我只可以由item_id查找表modifier_categories匹配的cat_parent_id

當我運行刪除查詢時,只有modifier_categories表中的行被刪除,但在modifier_items表中沒有被刪除。

以下是我正在使用的查詢。我至少看了好幾百遍,注意到似乎在工作。

任何幫助將非常感激。感謝先進

DELETE mc 
FROM modifier_categories mc 
INNER JOIN modifier_items mi 
ON mi.cat_parent_id=mc.cat_id 
WHERE mc.menu_item_id = 28560 
+0

如果你想這樣做沒有任何意義,我 – Drew

+0

[級聯刪除](http://stackoverflow.com/a/322984 05),然後通過創建這些關係來完成它們。看到這個參考,它的後半部分。你沒有顯示你的模式(爲每個xyz使用'show create table xyz')。您不會告訴您的查詢在刪除第二個表中的行時做任何事情。 – Drew

+0

你目前正在做的是一個類比。你有一張「人」桌子和一張「寵物」桌子。在你的40人中,可能會有一個叫保羅的人。現在,將人們加入寵物,完成後刪除Paul。 – Drew

回答

1

模式:

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; 
+0

嗨德魯。非常感謝這個例子。您指定的查詢只刪除了modifier_items表中的行,但未刪除modifier_category表中的行。 我找到了一個解決方案,可以在.. – json2021

+0

好吧,然後一個upvote顯示我很有幫助。這就是我們如何在這裏滾動。 – Drew

1

好了,所以我找到了答案。我想顯式指定兩個表,我想執行刪除。此代碼工作

 $query = "DELETE modifier_categories,modifier_items FROM modifier_categories INNER JOIN modifier_items ON 
modifier_items.cat_parent_id = modifier_categories.cat_id WHERE modifier_categories.menu_item_id = $item_id "; 
1

爲了從mc表中刪除記錄(出連接結果的)

DELETE mc 
FROM modifier_categories mc 
INNER JOIN modifier_items mi 
ON mi.cat_parent_id=mc.cat_id 
WHERE mc.menu_item_id = 28560 

爲了從mi表中刪除記錄(出來的連接結果)

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 

爲了刪除來自mcmi表記錄(出連接結果的)可能是你正在尋找這個

DELETE mc,mi 
FROM modifier_categories mc 
INNER JOIN modifier_items mi 
ON mi.cat_parent_id=mc.cat_id 
WHERE mc.menu_item_id = 28560 

請大家看看類似post

+0

在參與其中之前我沒有注意到答案。 :(順便說一句,爲寵物和人+1。 – 1000111

+0

沒關係我,我的心情很糟糕。 – Drew