File tree Expand file tree Collapse file tree 1 file changed +19
-24
lines changed
sql-queries-11/how-to-interpret-and-fix-the-MySQL-Error-1093 Expand file tree Collapse file tree 1 file changed +19
-24
lines changed Original file line number Diff line number Diff line change 1
1
2
2
-- 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);
6
6
7
7
-- This statement fixes the Error 1093 in the preceding example using a derived table
8
8
DELETE FROM Program
9
- WHERE id IN (
10
- SELECT subquery_program .id
9
+ WHERE start_date > (
10
+ SELECT subquery_program .start_date
11
11
FROM (
12
- SELECT id
13
- FROM Program
14
- WHERE department_id= 1
12
+ SELECT MIN (start_date) AS start_date FROM Program
15
13
) AS subquery_program
16
14
);
17
15
18
16
19
17
-- 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;
25
22
26
23
-- Delete from the main table using the temporary table
27
24
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 );
30
27
31
28
-- Drop the temporary table
32
- DROP TEMPORARY TABLE IF EXISTS temp_ids ;
29
+ DROP TEMPORARY TABLE temp_min_start_date ;
33
30
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 ;
You can’t perform that action at this time.
0 commit comments