|
1 | 1 | -- This statement generates the Error 1093
|
2 |
| -UPDATE Program |
3 |
| -SET end_date = (SELECT start_date FROM Program); |
| 2 | +UPDATE Program t |
| 3 | +SET t.end_date = (SELECT start_date FROM Program WHERE id=t.id); |
4 | 4 |
|
5 | 5 | -- This statement fixes the Error 1093 in the preceding example using a derived table
|
6 |
| -UPDATE Program |
7 |
| -SET end_date = ( |
8 |
| - SELECT subquery_program.start_date |
9 |
| - FROM ( |
10 |
| - SELECT start_date |
11 |
| - FROM Program |
12 |
| - WHERE description ='Major in Operating Systems' |
| 6 | +UPDATE Program t |
| 7 | +SET t.end_date = ( |
| 8 | + SELECT subquery_program.start_date FROM ( |
| 9 | + SELECT id, start_date FROM Program WHERE id=t.id |
13 | 10 | ) AS subquery_program
|
14 | 11 | );
|
15 | 12 |
|
16 |
| --- This statement generates the Error 1093 |
17 |
| -UPDATE Program |
18 |
| -SET name = (SELECT description FROM Program); |
19 |
| - |
20 |
| --- This set of statements fixes the Error 1093 in the preceding example using a temporary table |
| 13 | +-- This set of statements fixes the Error 1093 in the same example using a temporary table |
21 | 14 | -- Create a temporary table
|
22 |
| -CREATE TEMPORARY TABLE IF NOT EXISTS temp_table AS |
23 |
| -SELECT id, description |
| 15 | +CREATE TEMPORARY TABLE |
| 16 | +IF NOT EXISTS temp_table |
| 17 | +AS SELECT id, start_date |
24 | 18 | FROM Program;
|
25 | 19 |
|
26 | 20 | -- Update the main table using the temporary table
|
27 |
| -UPDATE Program t |
28 |
| -JOIN temp_table temp ON t.id = temp.id |
29 |
| -SET t.name = temp.description; |
| 21 | +UPDATE Program t |
| 22 | +JOIN temp_table temp |
| 23 | +ON t.id = temp.id |
| 24 | +SET t.end_date = temp.start_date; |
30 | 25 |
|
31 | 26 | -- Drop the temporary table
|
32 |
| -DROP TEMPORARY TABLE IF EXISTS temp_table; |
| 27 | +DROP TEMPORARY TABLE |
| 28 | +IF EXISTS temp_table; |
33 | 29 |
|
34 | 30 | -- This statement fixes the Error 1093 using a self-join
|
35 |
| -UPDATE Program p1 |
36 |
| -JOIN Program p2 ON p1.id = p2.id |
37 |
| -SET p1.name = p2.description; |
| 31 | +UPDATE Program p1 |
| 32 | +JOIN Program p2 |
| 33 | +ON p1.id = p2.id |
| 34 | +SET p1.end_date = p2.start_date; |
0 commit comments