We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 781e267 commit 91a9338Copy full SHA for 91a9338
sql-queries-12/updating-rows-subquery-referencing-same-table/mysql.sql
@@ -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