Skip to content

Commit 91a9338

Browse files
authored
Create mysql.sql
1 parent 781e267 commit 91a9338

File tree

1 file changed

+18
-0
lines changed
  • sql-queries-12/updating-rows-subquery-referencing-same-table

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
-- Using a Self Join
2+
UPDATE Program AS P1
3+
JOIN (
4+
SELECT id, DATE_ADD(start_date, INTERVAL 3 WEEK) AS new_end_date
5+
FROM Program
6+
) AS P2
7+
ON P1.id = P2.id
8+
SET P1.end_date = P2.new_end_date;
9+
10+
-- Using a Derived Table
11+
UPDATE Program
12+
SET end_date = (
13+
SELECT new_end_date FROM (
14+
SELECT DATE_ADD(start_date, INTERVAL 3 WEEK) AS new_end_date
15+
FROM Program AS Temp
16+
WHERE Temp.id = Program.id
17+
) AS Temp_table
18+
);

0 commit comments

Comments
 (0)