I have two tables TABLE1 contains columns (deptno,sal) and TABLE2 contains columns (deptno and location) now I want to update 1000 more salary in the sal column for those persons that are working in PARIS(location column table 2) could you please answer me how I can do the update?
Try this:
UPDATE t1 SET t1.sal = t1.sal+1000
From Table1 as t1 INNER JOIN Table2 as t2 On t1.deptno = t2.deptno
WHERE t2.location = 'PARIS'
Related
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
I am having two tables, table1 having three columns (id,names,salary) and table2 having three columns(id,names,totalsalary). I want to update table2 totalsalary column by using table1. Please, find the below attachment of tables in images
You could do inner join both tables and update totalsalary column on table2 using salary column from table1
update t2
set t2.totalsalary = t1.salary * 12 (Some calculation if needed)
from table2 t2
inner join table1 t1 on t1.id = t2.id
It seems you want to UPDATE the salary value in table2 with the sum of the salaries in table1. The most straight forward way to do this is with a sub-query and a JOIN.
The sub-query:
SELECT
id,
SUM(salary) as TotalSalary
FROM
table1;
And then the JOIN:
UPDATE t2
SET
salary = t1.TotalSalary
FROM
table2 AS t2
JOIN
(SELECT
id,
SUM(salary) as TotalSalary
FROM
table1
) AS t1
ON t1.id = t2.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;
I wanted to update table1 based on table2 which has a common field employee_ID. I always have unique employee_ID in table1 but in table2 it may contain duplicate records with same employee_ID.
I got another column in table2 where the modified_date is inserted. I want to update the table1 employee name with the table2 employee name based on employee_id and recent modified date.
I got multiple columns to update with the same type of condition. any idea, here is so far i tried.
Here is the query, i have using inner join,
ssql = "Update Table1 INNER JOIN Table2
ON Table1.employee_id= Table2.employee_id
SET Table1.type= Table2.type"
any help will be appreciated
try this query
update t1 set t1.employee_name = t2.employee_name from table1 as t1
inner join table2 as t2 on t1.employee_id = t2.employee_id
where t2.Modified_date = (select Max(modified_date) from table2 as tb2 where
tb2.employee_id = t2.employee_id group by tb2.employee_id)
You need an intermediate step to associate the Max(modified_date) to each employee_id.
Using a CTE expression you can try something like this :
with
more_recent as (
select
employee_id,
Max(modified_date) max_modified_date
from
table2
group by
employee_id
)
update
t1
set
t1.employee_name = t2.employee_name
from
table1 as t1
inner join more_recent mr on
t1.employee_id = mr.employee_id
inner join table2 as t2 on
mr.employee_id = t2.employee_id
mr.max_modified_date = t2.modified_date
table1 has column CITY and COUNTRY. table2 has column CITY.
how do i delete from table2 all records that have CITY in common with table1 but also the COUNTRY='Russia' ??
please keep in mind that both tables have about 1 million rows of data
DELETE table2
FROM table2 INNER JOIN table1
ON table2.CITY = table1.CITY
WHERE table1.COUNTRY = 'Russia'
You can use the multitable delete syntax:
DELETE table2
FROM table1
JOIN table2
ON table1.city = table2.city
WHERE table1.country = 'RUSSIA'