我嘗試在一箇中刪除查詢。例如:MySQL多表刪除語法
DELETE FROM `comments` WHERE `article_id` = 4;
DELETE FROM `aricles` WHERE `id` = 4;
我試圖用一個單一的查詢:
DELETE `articles`, `comments`
FROM `articles` INNER JOIN `comments`
WHERE `comments`.`article_id` = `articles`.`id` AND `articles`.`id` = 4
如果表comments
存在的記錄與article_id
4行之有效,但不與articles
刪除articles
記錄。 id
= 4,如果在comments
記錄中article_id
= 4未找到。有沒有辦法做到這一點?
你真的應該看看約束。請參閱http://stackoverflow.com/questions/3433975/why-use-foreign-key-constraints-in-mysql – Rufinus 2012-03-13 11:37:11