Skip to content

Commit 73f8c21

Browse files
authored
Update fix-when-using-DELETE-statement.sql
1 parent 632b1b6 commit 73f8c21

File tree

1 file changed

+19
-24
lines changed

1 file changed

+19
-24
lines changed
Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,36 @@
11

22
-- This statement generates the Error 1093
3-
DELETE FROM Program
4-
WHERE id IN
5-
(SELECT id FROM Program WHERE department_id=1);
3+
DELETE FROM Program
4+
WHERE start_date >
5+
(SELECT MIN(start_date) FROM Program);
66

77
-- This statement fixes the Error 1093 in the preceding example using a derived table
88
DELETE FROM Program
9-
WHERE id IN (
10-
SELECT subquery_program.id
9+
WHERE start_date > (
10+
SELECT subquery_program.start_date
1111
FROM (
12-
SELECT id
13-
FROM Program
14-
WHERE department_id=1
12+
SELECT MIN(start_date) AS start_date FROM Program
1513
) AS subquery_program
1614
);
1715

1816

1917
-- 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;
18+
- Create a temporary table
19+
CREATE TEMPORARY TABLE temp_min_start_date AS
20+
SELECT MIN(start_date) AS min_date
21+
FROM Program;
2522

2623
-- Delete from the main table using the temporary table
2724
DELETE FROM Program
28-
WHERE id
29-
IN (SELECT id FROM temp_ids);
25+
WHERE start_date >
26+
(SELECT min_date FROM temp_min_start_date);
3027

3128
-- Drop the temporary table
32-
DROP TEMPORARY TABLE IF EXISTS temp_ids;
29+
DROP TEMPORARY TABLE temp_min_start_date;
3330

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;
31+
-- This statement fixes the Error 1093 using a join with a derived table
32+
DELETE p
33+
FROM Program AS p
34+
JOIN (SELECT MIN(start_date) AS min_start_date FROM Program)
35+
AS min_program_date
36+
ON p.start_date > min_program_date.min_start_date;

0 commit comments

Comments
 (0)