how can i update a table column from another table. here is my code :
update table1 hn
set hn.changeColumn=es.changeColumn
from table1 hn
inner join table2 es on es.x=hn.xand es.rol_id=hn.rol_id
where hn.x= es.x and hn.rol_id = es.rol_id
i wanna set table1's column(changeColumn) values with table2's column(changeColumn) values
how can i do this. thanks
If you want to convert this query to Oracle, You need a MERGE INTO statement -
MREGE INTO table1 hn
USING table2 es
ON (es.x=hn.x and es.rol_id=hn.rol_id)
WHEN MATCHED THEN
UPDATE
SET hn.changeColumn=es.changeColumn
One valid way to write this query on Oracle would be:
UPDATE table1 t1
SET changeColumn = (SELECT t2.changeColumn
FROM table2 t2
WHERE t1.x = t2.x AND t1.rol_id = t2.rol_id);
This assumes that your join condition would only generate at most a single pair of mstching records from the self join. If not, then we would have to modify the logic.
I have two tables whose relevant columns look more or less like the following:
table1.id:
zIXuJeY-qH
zJrcULGLXK
zXyIgnlylb
zZ7MYDGDAV
zbCFI4wKpe
ziGeauO-0O
zmzWbaGSb7
table2.id
zIXuJeY-qH
zIXuJeY-qH
zIXuJeY-qH
zIXuJeY-qH
zJrcULGLXK
zJrcULGLXK
in which table1.id is a primary key and table2.id is not a primary key. I'm trying to grab all the IDs from table 1 that are not in table 2, but am having trouble. If, for instance, I select a count of all the values that are in table two, I get:
>SELECT COUNT(id) FROM table1 WHERE id IN (SELECT id FROM table2);
>2
But if I try to find the IDs not in table2, I get 0:
>SELECT COUNT(id) FROM table1 WHERE id NOT IN (SELECT id FROM table2);
>
There are 503 unique IDs in table1 and only 163 in table2. What am I doing wrong?
I suppose there can be nulls in table2.id?
NOT IN ( ..., null, ...)
does never result in true unfortunately (because - so the argumentation - null is the unknown value, and we cannot guarantee that the unknown value is not the value you are looking for obviously). A bad trap.
Try
SELECT COUNT(id) FROM table1 WHERE id NOT IN (SELECT id FROM table2 where id is not null);
or an EXISTS clause instead.
I am trying to update one column in table1 from a column in table2. Here
is what i am doing but i am getting an ORA error.
ORA-01427: single-row subquery returns more than one row.
update table1 a
set a.art_num = (
select b.art_num from table2 b
where a.comp_id = b.comp_id );
Thanks so much in advance!
This happens because your subquery returns more than one result.
You should check this one:
select b.art_num
from table2 b
where a.comp_id = b.comp_id
You could try to select DISTINCT (search for distinct in the link for documentation) on the subquery:
update table1 a
set a.art_num = (
select distinct(b.art_num)
from table2 b
where a.comp_id = b.comp_id );
I am trying to update a selected values in a column in a SQLite table. I only want update of the cells in the maintable where the criteria are met, and the cells must be updated to individual values, taken from a subtable.
I have tried the following syntax, but I get only a single cell update. I have also tried alternatives where all cells are updated to the first selected value of the subtable.
UPDATE maintable
SET value=(SELECT subtable.value FROM maintable, subtable
WHERE maintable.key1=subtable.key1 AND maintable.key2=subtable.key2)
WHERE EXISTS (SELECT subtable.value FROM maintable, subtable
WHERE maintable.key1=subtable.key1 AND maintable.key2=subtable.key2)
What is the appropriate syntax?
You can do this with an update select, but you can only do one field at a time. It would be nice if Sqlite supported joins on an update statement, but it does not.
Here is a related SO question, How do I UPDATE from a SELECT in SQL Server?, but for SQL Server. There are similar answers there.
sqlite> create table t1 (id int, value1 int);
sqlite> insert into t1 values (1,0),(2,0);
sqlite> select * from t1;
1|0
2|0
sqlite> create table t2 (id int, value2 int);
sqlite> insert into t2 values (1,101),(2,102);
sqlite> update t1 set value1 = (select value2 from t2 where t2.id = t1.id) where t1.value1 = 0;
sqlite> select * from t1;
1|101
2|102
In this case, it only updates one value from subtable per each raw from maintable.
The error is when subtable is include into of SELECT sentence.
UPDATE maintable
SET value=(SELECT subtable.value
FROM subtable
WHERE maintable.key1=subtable.key1 );
By default update with joins does not exist in SQLite; But we can use the with-clause + column-name-list + select-stmt from https://www.sqlite.org/lang_update.html to make something like this:
CREATE TABLE aa (
_id INTEGER PRIMARY KEY,
a1 INTEGER,
a2 INTEGER);
INSERT INTO aa VALUES (1,10,20);
INSERT INTO aa VALUES (2,-10,-20);
INSERT INTO aa VALUES (3,0,0);
--a bit unpleasant because we have to select manually each column and it's just a lot to write
WITH bb (_id,b1, b2)
AS (SELECT _id,a1+2, a2+1 FROM aa WHERE _id<=2)
UPDATE aa SET a1=(SELECT b1 FROM bb WHERE bb._id=aa._id),a2=(SELECT b2 FROM bb WHERE bb._id=aa._id)
WHERE _id in (SELECT _id from bb);
--soo now it should be (1,10,20)->(1,12,21) and (2,-10,-20)->(2,-8,-19), and it is
SELECT * FROM aa;
--even better with one select for each row!
WITH bb (_id,b1, b2)
AS (SELECT _id,a1+2, a2+1 from aa WHERE _id<=2)
UPDATE aa SET (a1,a2)=(SELECT b1,b2 FROM bb WHERE bb._id=aa._id)
WHERE _id in (SELECT _id from bb);
--soo now it should be (1,12,21)->(1,14,22) and (2,-8,-19)->(2,-6,-18), and it is
SELECT * FROM aa;
--you can skip the WITH altogether
UPDATE aa SET (a1,a2)=(SELECT bb.a1+2, bb.a2+1 FROM aa AS bb WHERE aa._id=bb._id)
WHERE _id<=2;
--soo now it should be (1,14,22)->(1,16,23) and (2,-6,-18)->(2,-4,-17), and it is
SELECT * FROM aa;
Hopefully sqlite is smart enough to not query incrementally but according to the documentation it is. When setting multiple columns using one select (case 2 and 3) a not valid id (no where _id in line) will give an error that can not be ignored using ON IGNORE, case 1 will set columns to null (for all ids >2) which is also bad.
You need to use an INSERT OR REPLACE statement, something like the following:
Assume maintable has 4 columns: key, col2, col3, col4
and you want to update col3 with the matching value from subtable
INSERT OR REPLACE INTO maintable
SELECT maintable.key, maintable.col2, subtable.value, maintable.col4
FROM maintable
JOIN subtable ON subtable.key = maintable.key
I'd like to do something like this:
Suppose I have two tables, myTable1 and myTable2. Suppose both of these tables have columns myColumn1 and myColumn2.
update
myTable1
set
myTable1.myFlagColumn = 1
where
myTable1.myColumn1, myTable1.myColumn2
in
(select myTable2.myColumn1, myTable2.myColumn2 from myTable2)
Essentially, I want to change a value in myTable1 if there are any rows where the two columns in myTable1 and myTable2 match.
Is this possible?
Yes, but you will use an EXISTS clause:
update
myTable1
set
myTable1.myFlagColumn = 1
where
EXISTS
(select * FROM myTable2 WHERE myTable2.myColumn1 = myTable1.myColumn1
AND myTable2.myColumn2 = myTable1.myColumn2)