Update table values from another table with the same user name - sql

I have two tables, with a same column named user_name, saying table_a, table_b.
I want to, copy from table_b, column_b_1, column_b2, to table_b1, column_a_1, column_a_2, respectively, where the user_name is the same, how to do it in SQL statement?

As long as you have suitable indexes in place this should work alright:
UPDATE table_a
SET
column_a_1 = (SELECT table_b.column_b_1
FROM table_b
WHERE table_b.user_name = table_a.user_name )
, column_a_2 = (SELECT table_b.column_b_2
FROM table_b
WHERE table_b.user_name = table_a.user_name )
WHERE
EXISTS (
SELECT *
FROM table_b
WHERE table_b.user_name = table_a.user_name
)
UPDATE in sqlite3 did not support a FROM clause for a long time, which made this a little more work than in other RDBMS. UPDATE FROM was implemented in SQLite 3.33 however (2020-08-14) as mentioned at: https://stackoverflow.com/a/63079219/895245
If performance is not satisfactory, another option might be to build up new rows for table_a using a select and join with table_a into a temporary table. Then delete the data from table_a and repopulate from the temporary.

Starting from the sqlite version 3.15 the syntax for UPDATE admits a column-name-list
in the SET part so the query can be written as
UPDATE table_a
SET
(column_a_1, column_a_2) = (SELECT table_b.column_b_1, table_b.column_b_2
FROM table_b
WHERE table_b.user_name = table_a.user_name )
which is not only shorter but also faster
the last "WHERE EXISTS" part
WHERE
EXISTS (
SELECT *
FROM table_b
WHERE table_b.user_name = table_a.user_name
)
is actually not necessary

It could be achieved using UPDATE FROM syntax:
UPDATE table_a
SET column_a_1 = table_b.column_b_1
,column_a_2 = table_b.column_b_2
FROM table_b
WHERE table_b.user_name = table_a.user_name;
Alternatively:
UPDATE table_a
SET (column_a_1, column_a_2) = (table_b.column_b_1, table_b.column_b_2)
FROM table_b
WHERE table_b.user_name = table_a.user_name;
UPDATE FROM - SQLite version 3.33.0
The UPDATE-FROM idea is an extension to SQL that allows an UPDATE statement to be driven by other tables in the database. The "target" table is the specific table that is being updated. With UPDATE-FROM you can join the target table against other tables in the database in order to help compute which rows need updating and what the new values should be on those rows

There is an even much better solution to update one table from another table:
;WITH a AS
(
SELECT
song_id,
artist_id
FROM
online_performance
)
UPDATE record_performance
SET
op_song_id=(SELECT song_id FROM a),
op_artist_id=(SELECT artist_id FROM a)
;

Update tbl1
Set field1 = values
field2 = values
Where primary key in tbl1 IN ( select tbl2.primary key in tbl1
From tbl2
Where tbl2.primary key in tbl1 =
values);

The accepted answer was very slow for me, which is in contrast to the following:
CREATE TEMPORARY TABLE t1 AS SELECT c_new AS c1, table_a.c2 AS c2 FROM table_b INNER JOIN table_a ON table_b.c=table_a.c1;
CREATE TEMPORARY TABLE t2 AS SELECT t1.c1 AS c1, c_new AS c2 FROM table_b INNER JOIN t1 ON table_b.c=t1.c2;

Related

ORACLE - Update a value from two different columns in differente tables with a filter

I'have a little question about a query.
I have to update a column from a table where there are only record of expense(integer).
I must increase the expense of 5% if the client is from a specific state, the column of the state is in a different table and the key in common is the address.
This is my query below :
UPDATE table 1 a
SET expense_vl = (
SELECT expense*1.05 FROM table 1
LEFT JOIN table2 b ON b.ADDRESS_ID=a.ADDRESS_ID
WHERE description_state IN 'lollyland'
)
I'd recommend using a semi-join:
update table_1 a
set expense_v1 = expense * 1.05
where exists (
select null
from table2 b
where
a.address_id = b.address_id and
b.description_state = 'lollyland'
)
Althought I must add that it would help if you include the DDL for your table. We're sort of guessing at which table "description" came from.
Also, when possible, include sample input for each table and desired output. We don't need a million records, just an example that illustrates your issue.
Try below
UPDATE table1 a SET expense_vl = (SELECT expense*1.05
FROM table2 b
WHERE b.ADDRESS_ID=a.ADDRESS_ID)
WHERE description_state IN 'lollyland'
Or try with subselect:
UPDATE table1
SET expense_vl = expense*1.05
WHERE ADDRESS_ID IN (SELECT ADDRESS_ID FROM table2 WHERE description_state IN 'lollyland')
I think you need to change your query like below :
UPDATE table 1 A
SET expense_vl=expense*1.05 FROM table 1
LEFT JOIN table2 B ON B.ADDRESS_ID=A.ADDRESS_ID
WHERE B.description_state IN 'lollyland'

SQL insert data with inner join using oracle

New to SQL I have two tables table_A and table_B.
I want to add data into a specific column of table_A depending on a inner join on table_B. I am using Oracle, following this method my SQL looks like this :
I first tried that :
INSERT INTO table_A (target_column)
SELECT table_B.wanted_data
FROM table_B INNER JOIN table_A ON table_B.someColumnB = table_A.someColumnA
Here the issue is that it would insert the data in new lines of my table_A and not in the existing lines.
So I tried that from the stackoverflow thread :
UPDATE(SELECT table_A.target_column, table_B.wanted_data
FROM table_A
INNER JOIN table_B
ON table_A.someColumnA = table_B.someColumnB
)
SET table_A.target_table = table_B.wanted_data
But it is not working either "SQL command not properly ended"
EDIT : target_column and wanted_data have the same name in my data set, not sure if it changes anything.
SQL Sever:
UPDATE a
SET a.target_column = b.wanted_data
FROM table_A a
JOIN table_B b
ON b.someColumnB = a.someColumnA
Oracle:
UPDATE
(
SELECT b.wanted_data AS wanted_data
,a.target_column AS old_data
FROM table_A a
INNER JOIN table_B b
ON b.someColumnB = a.someColumnA
) c
SET c.old_data = c.wanted_data
Regarding this approach, which you said fails with "SQL command not properly ended":
(This is the failing code:)
update
( select table_a.target_column
, table_b.wanted_data
from table_a
join table_b
on table_b.somecolumnb = table_a.somecolumna
)
set table_a.usineid = table_b.usineid;
I can't see why you would get that error, but the last line is incorrect because labels table_a and table_b only exist inside the inline view, which is anonymous. (Also there is no UsineId but I am assuming that is a copy-paste issue in your example.) It needs to be either:
update
( select table_a.target_column
, table_b.wanted_data
from table_a
join table_b
on table_b.somecolumnb = table_a.somecolumna
) -- you are updating an anonymous inline view
set target_column = wanted_data;
or
update
( select table_a.target_column
, table_b.wanted_data
from table_a
join table_b
on table_b.somecolumnb = table_a.somecolumna
) v -- you are updating an inline view named V
set v.target_column = v.wanted_data;
Then, there needs to be a unique index or constraint on the parent key, table_b.somecolumnb, otherwise Oracle will refuse to attempt any update with
ORA-01779: cannot modify a column which maps to a non key-preserved table
Alternatively, you can use merge:
merge into table_a a
using table_b b
on (b.somecolumnb = a.somecolumna)
when matched then update set a.target_column = b.wanted_data;
Give this a try:
update table_A A
set A.target_column = (
select table_B_data
from table_B
where shared_key = A.shared_key
);
This will only work if there is one row with table_B_date in table_B. If there are multiple rows, you need to specify the key

Updating a key on table from another table in Oracle

I am trying to update a key on a table (t1) when the key value is (abc) by getting the value from table (t2).
It is working as expected when I am limiting it to a specific person
update table_a t1
set t1.u_key = (select t2.u_key
from table_b t2
where t2.name_f=t1.name_f
and t2.name_l=t1.name_l
and rownum<=1
and t2='NEVADA')
where t1.u_key = 'abc'
and e.name_f='Lori'
and e.name_l='U'
;
I initially tried without rownum and it said too many rows returned.
To run on all the data with t1.u_key='abc' and took out the specific name, I tried this which has been running until time out.
update table_a t1
set t1.u_key = (select t2.u_key
from table_b t2
where t2.name_f=t1.name_f
and t2.name_l=t1.name_l
and rownum<=1
and t2='NEVADA')
where t1.u_key = 'abc'
;
Can you please look at it and suggest what am I missing.
You should first take a look what is returned when you run the inner SELECT statement alone:
SELECT t2.u_key FROM table_b t2
WHERE t2.name_f IN (SELECT name_f FROM table_a WHERE u_key = 'abc')
AND t2.name_l IN (SELECT name_l FROM table_a WHERE u_key = 'abc')
AND t2='NEVADA'
Examine the results and you will see that there are more than one row returned.
If there should be only matching row per key, you would need to add the key to the inner SELECT as well but I can't tell you how it should look like without additional table descriptions and possibly some sample entries from table_a and table_b.
Use this:
update (
SELECT t2.u_key t2key,
t1.ukey t1key
FROM table_b t2,
table_a t1
where t2.name_f=t1.name_f
and t2.name_l=t1.name_l
and t2='NEVADA'
and rownum<=1 )
SET t1key = t2key
where t1key = 'abc';
merge into table_a t1
using(
select name_f, name_l, max(u_key) as new_key
from table_b t2
where t2='NEVADA'
group by name_f, name_l
) t2
on (t1.name_f=t2.name_f and t1.name_l=t2.name_l and t1.u_key='abc')
when matched then
update set t1.u_key=t2.new_key

Selecting few columns as table

I have a problem in designing a query:
I have to select few records based on criteria
(SELECT COL_1, COL_2,COL_3 FROM TABLE_1 WHERE COL_3 = 'CND')
Now I need to select records from two databases based on these results
(SELECT XX_1, XX_2
FROM TABLE_2 WHERE TABLE_2.XX1 = TABLE_1.COL1
(from filtered results in step 1)
(SELECT YY_1, YY_2, YY_3
FROM TABLE_3 WHERE TABLE_3.YY_2 = TABLE_1.COL2)
(from filtered results in step 1)
I need results in single table view
XX_1, XX_2, YY_1, YY_2, YY_3
mentioned columns must be equal to be in result and only record with such equality should be fetched.
I need to run this on millions of records, so performance is considered
It's gonna be used in Java classes, so please don't suggest me any db specific/sql commands which can't be executed since I don't hold any db permissions other than read.
Hope I am clear. In case not, I will explain the doubts.
I tried something like this
SELECT *
FROM TABLE_2
JOIN
(SELECT COL_1,
COL_2,
COL_3
FROM TABLE_1
WHERE COL_3 = 'CND'
GROUP BY COL_1) TMP_TABLE
ON (TMP_TABLE.COL_1 = TABLE2.XX_1)
But I got view/table doesn't exists - oracle error.
I think you need to use a subquery,just like this
select col_1,col_2
from(
select col_1,col_2
from (
select col_1,col_2 from table_1
)tbl1
left join table_2 tbl2 on tbl2.col_1 = tbl1.col_1
)tbl3
left join table_3 tbl3 on tbl4.col_1 = tbl3.col_1
with usedrows as
( select a.Col_1,a.Col_2 FROM table1 a left JOIN table2 b ON a.Col_1=b.Col_2)
select Col_1,C0l_2 from usedrows
This is just an example where usedrows is a virtual table made after join.and u can select the columns from that join table as u select from other table.

refer to outside field value in subselect?

I want to do a query to update values that I forgot to copy over in a mass insert. However I'm not sure how to phrase it.
UPDATE table
SET text_field_1 = (SELECT text_field_2
FROM other_table
WHERE id = **current row in update statement, outside parens**.id )
How do I do this? It seems like a job for recursion.
Use:
UPDATE YOUR_TABLE
SET text_field_1 = (SELECT t.text_field_2
FROM other_table t
WHERE t.id = YOUR_TABLE.id)
Warning
If there's no supporting record in other_table, text_field_1 will be set to NULL.
Explanation
In standard SQL, you can't have table aliases on the table defined for the UPDATE (or DELETE) statement, so you need to use full table name to indicate the source of the column.
It's called a correlated subquery -- the correlation is be cause of the evaluation against the table from the outer query.
Clarification
MySQL (and SQL Server) support table aliases in UPDATE and DELETE statement, in addition to JOIN syntax:
UPDATE YOUR_TABLE a
JOIN OTHER_TABLE b ON b.id = a.id
SET a.text_field_1 = b.text_field_2
...is not identical to the provided query, because only the rows that match will be updated -- those that don't match, their text_field_1 values will remain untouched. This is equivalent to the provided query:
UPDATE YOUR_TABLE a
LEFT JOIN OTHER_TABLE b ON b.id = a.id
SET a.text_field_1 = b.text_field_2
If there is one ID field:
UPDATE updtable t1
SET t1.text_field_1 = (
SELECT t2.text_field_2
FROM seltable t2
WHERE t1.ID = t2.ID
)
;
UPDATE Table1, Tabl2
SET Table1.myField = Table2.SomeField
WHERE Table1.ID = Table2.ID
Note: I have not tried it.
This will only update records where IDs match.
Try this:
UPDATE table
SET text_field_1 = (SELECT text_field_2
FROM other_table
WHERE id = table.id )