Table 1
Id | place | expiry_date |
---|---|---|
10 | xyz | 2022-09-12 |
Table 2 - expiry_date is the new column created in table 2. Need to fetch expiry date from table 1 where T1_id (in table 2) matches id (in table 1)
Oid | userid | expiry_date | T1_id |
---|---|---|---|
2 | 123 | 10 |
How to fetch expiry date (table 1 and fill the new column in table 2) only if the T1_id and Id(table 1) matches
Trying
insert into (sql) statements
Joins used
Join table1.Id on table2.T1_id
CodePudding user response:
Insert statements won't allow you to change values from an existing table, they only allow you to add brand new rows to a table. In your case you may want to use an UPDATE
statement.
In order to get matches between the two tables, you can apply a JOIN
operation within the UPDATE
statement, using the condition you pointed in your post description.
UPDATE tab2
INNER JOIN tab1
ON tab1.Id = tab2.T1_id
SET tab2.expiry_date = tab1.expiry_date;
Check the demo here.