Move data from tb1 to tbl2 where - sql

I need to move data from column reg in table1 to reg2 in table2, where the ID of table1 = 'ID2' of table2.
basically, I need to shift car registrations from A to B as long as the person exists in B.

update table column by using join
update t2
set t2.reg2=t1.reg
from table2 t2 inner join table1 t1
on t2.ID2=t1.ID

INSERT INTO table2 (reg2)
SELECT reg
FROM table1
WHERE table2.ID2 = table1.ID;

Related

SQL how to insert from another table based on same row values?

I have two tables with similar column values for columns "name" and "regis". One table is already filled with values, the other has only values for "name". How do i get "regis" values from table1 into table2 based on same column values?
I tried something like this
INSERT INTO table2 (regis)
SELECT table1 (regis)
WHERE table2.name = table1.name
Please try:
update table2 set regis = (select regis from table1 where table1.name=table2.name);
see the whole sample on:
https://www.db-fiddle.com/f/3jC8PGeZZEuty3XVq8gdzz/9
I think, what you are after is insert when name not found in table2 or update if its found in both tables.
INSERT INTO TABLE2 (regis)
SELECT t1.REGIS FROM TABLE1 as t1
LEFT OUTER JOIN TABLE2 as t2 on t2.name = t1.name
WHERE t2.name is null
You can update table2 using the statement like below:
update t2
set t2.regis = t1.regis
from table2 as t2
inner join table1 as t1 on t1.name = t2.name
If you are using SQL Server, then you can use merge statement to either insert or update in one step.
You need to use table2 as join in your select statement.
INSERT INTO table2 (regis)
SELECT table1.regis FROM Table1 INNER JOIN Table2 on table1.name = table2.name

Update table column based on another table based on ID

I have 2 tables 'table1' and 'table2'.
table1 has 10,000 records and table2 has 5,000 records.
Both tables have "RECORD_ID" column. All RECORD_ID's that are in table2 can be found in table1.
I want to UPDATE the "PRICE" column of table1 based on the "RECORD_ID" column of table2.
update table1 set PRICE = table2.PRICE where RECORD_ID = table2.RECORD_ID
I got this error message:
SQL0206N "table2.PRICE" is not valid in the context where it is used
SQLSTATE=42703
I am using DB2.
UPDATE table1 SET table1.price = (SELECT table2.price FROM table2 WHERE table2.record_id = table1.record_id)
Try this:
UPDATE table1 f1
SET f1.price =(
SELECT f2.price
FROM table2 f2
WHERE f2.record_id = f1.record_id
)
WHERE exists
(
SELECT f2.price
FROM table2 f2
WHERE f2.record_id = f1.record_id
)
You have to use a join like this:
UPDATE
Table1
SET
Table1.Column = T2.Column
FROM
Table1 T1
INNER JOIN
Table2 T2
ON
T1.PK = T2.FK;

insert into table1 from table2 with condition

I have two tables, and I want to insert data in one of the new column (Orders) of table1 from table2. Any help please, below code is not working
Insert into Table1 (Orders)
Select (orders) from table2,
Where table1.id=table2.id
try this:
update t1
set t1.orders=t2.orders
from Table1 t1
inner join table2 t2
on t1.id=t2.id
UPDATE Table1
SET Orders = (
SELECT Orders
FROM Table2
WHERE table1.Id = table2.Id
)

SQL Insert Data If Column A Matches and Column B Doesn't

I have a SQL Insert statement that needs to insert records into another table only if the the record doesn't exist in table2 or the zip code has changes in table1. I have tried the following but it throws an error and it is the logic I am looking for:
INSERT INTO table2
SELECT id, zip
FROM table1 t1
JOIN table2 t2
ON t1.id = t2.id and t1.zip <> t2.zip
I also need it to insert the records if the id doesn't exist at all in table2. I have googled the crap out of this and can't seem to find the solution anywhere.
What about this?
INSERT INTO table2
SELECT t2.id, t2.zip
FROM table1 t1
LEFT OUTER JOIN table2 t2
ON t1.id = t2.id
WHERE (t1.id IS NULL OR t2.zip <> t1.zip)
Also, be sure to clarify which table's id and zip columns you are asking for.
You should always include column lists when doing inserts. Second, your query doesn't quite capture your logic. You need a left outer join to find the records that don't exist in the second table. Perhaps this might do what you want:
INSERT INTO table2(id, zip)
SELECT id, zip
FROM table1 t1 LEFT JOIN
table2 t2
ON t1.id = t2.id
WHERE (t1.zip <> t2.zip) or (t2.zip is null)
You just need a WHERE NOT EXISTS clause
INSERT INTO table2
SELECT id, zip
FROM table1
WHERE NOT EXISTS (SELECT 1 FROM table2 WHERE table2.id = table1.id AND table2.zip = table1.zip)

Retrieving values from another table which has its relationship to the first table defined by a third table

Right now I have
SELECT
table1.field1 AS whatever
, table2.field2 AS stuff
FROM
table1
INNER JOIN table2 ON table1.goId = table2.Id
I need to be able to select a field3 which is located in a table3. Thing is, table1 has no direct foreign key pointing to table3, but it DOES have foreign key that points to a table which has another foreign key pointing to table3. How do I make the join between these 3 tables?
Given the lack of database structure provided the following could be used
SELECT
--Put column Declarations here--
table1.columnName1
,table2.columnName2
,table3.columnName3
FROM
table1
JOIN table2 ON table1.columnName1 = table2.columnName1
JOIN table4 ON table1.columnName1 = table4.columnName1
JOIN table3 ON table4.columnName4 = table3.columnName4
OR you could nest the third table
SELECT
--Put column Declarations here--
table1.columnName1
,table2.columnName2
,table3.columnName3
FROM
table1
JOIN table2 ON table1.columnName1 = table2.columnName1
JOIN table4
JOIN table3 ON table4.columnName4 = table3.columnName4
ON table1.columnName1 = table4.columnName1
Say that fourth table is table2.
SELECT
table3.column_You_want
FROM
table3
INNER JOIN table2 ON table2.column_linked_to_table3 = table3.column_name_linked_to_table2
INNER JOIN table1 ON table2.column_linked_to_table1 = table1.column_linked_to_table2