Skip to content

Commit 632b1b6

Browse files
authored
Update fix-when-using-UPDATE-statement.sql
1 parent ef703f0 commit 632b1b6

File tree

1 file changed

+20
-23
lines changed

1 file changed

+20
-23
lines changed
Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,34 @@
11
-- 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);
44

55
-- 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
1310
) AS subquery_program
1411
);
1512

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
2114
-- 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
2418
FROM Program;
2519

2620
-- 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;
3025

3126
-- Drop the temporary table
32-
DROP TEMPORARY TABLE IF EXISTS temp_table;
27+
DROP TEMPORARY TABLE
28+
IF EXISTS temp_table;
3329

3430
-- 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

Comments
 (0)