首先,您需要一個hierarchical query,用於檢索給定「Parent 1」的所有子行 - 這是Oracle數據庫的版本:。
SELECT *
FROM table
START WITH name = 'Parent 1'
CONNECT WITH PRIOR item_no = parent_item_no
現在的任務變得簡單:
DELETE FROM table
WHERE ItemNo IN (
SELECT ItemNo
FROM table
START WITH name = 'Parent 1'
CONNECT WITH PRIOR item_no = parent_item_no
)
一個版本的SQL服務器:
這是一個reqursive查詢給出Parent 1
所有後代 - >Demo
WITH q AS(
SELECT item_no, parent_item_no, name
FROM t
WHERE name = 'Parent 1'
UNION ALL
SELECT t.item_no, t.parent_item_no, t.name
FROM t
JOIN q
ON t.parent_item_no = q.item_no
)
SELECT * FROM q;
這DELETE語句使用上面的查詢,刪除下面Parent 1
WITH q AS(
SELECT item_no, parent_item_no, name
FROM t
WHERE name = 'Parent 1'
UNION ALL
SELECT t.item_no, t.parent_item_no, t.name
FROM t
JOIN q
ON t.parent_item_no = q.item_no
)
DELETE FROM t WHERE item_no IN (
SELECT item_no FROM q
);
演示了整個子樹:http://sqlfiddle.com/#!6/9a171/1
的[PHP MySQL的刪除父和子行]可能的複製(https://stackoverflow.com/questions/15747353/php-mysql-delete-parent-and-child-rows) –
對於Oracle請參閱[this](https://www.techonthenet.com/oracle/foreign_keys/foreign_delete.php)頁面。 –
只需向同事尋求幫助和輔導。 – Maritim