Skip to content

Commit ef703f0

Browse files
authored
Update fix-when-using-DELETE-statement.sql
1 parent d641326 commit ef703f0

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,41 @@
11

2+
-- This statement generates the Error 1093
3+
DELETE FROM Program
4+
WHERE id IN
5+
(SELECT id FROM Program WHERE department_id=1);
6+
7+
-- This statement fixes the Error 1093 in the preceding example using a derived table
8+
DELETE FROM Program
9+
WHERE id IN (
10+
SELECT subquery_program.id
11+
FROM (
12+
SELECT id
13+
FROM Program
14+
WHERE department_id=1
15+
) AS subquery_program
16+
);
17+
18+
19+
-- This set of statements fixes the Error 1093 in the example using a temporary table
20+
-- Create a temporary table
21+
CREATE TEMPORARY TABLE temp_ids AS
22+
SELECT id
23+
FROM Program
24+
WHERE department_id=1;
25+
26+
-- Delete from the main table using the temporary table
27+
DELETE FROM Program
28+
WHERE id
29+
IN (SELECT id FROM temp_ids);
30+
31+
-- Drop the temporary table
32+
DROP TEMPORARY TABLE IF EXISTS temp_ids;
33+
34+
-- This statement fixes the Error 1093 using a self-join
35+
DELETE t1
36+
FROM Program t1
37+
JOIN (
38+
SELECT id
39+
FROM Program
40+
WHERE department_id=1
41+
) AS t2 ON t1.id = t2.id;

0 commit comments

Comments
 (0)