Skip to content

Commit 0722d86

Browse files
authored
Create fix-when-using-UPDATE-statement.sql
1 parent 0902e62 commit 0722d86

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
-- This statement generates the Error 1093
2+
UPDATE Program
3+
SET end_date = (SELECT start_date FROM Program);
4+
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'
13+
) AS subquery_program
14+
);
15+
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
21+
-- Create a temporary table
22+
CREATE TEMPORARY TABLE IF NOT EXISTS temp_table AS
23+
SELECT id, description
24+
FROM Program;
25+
26+
-- 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;
30+
31+
-- Drop the temporary table
32+
DROP TEMPORARY TABLE IF EXISTS temp_table;
33+
34+
-- 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;

0 commit comments

Comments
 (0)