This is a common SQL query for me:
update table1 set col1 = (select col1 from table2 where table1.ID = table2.ID)
where exists (select 1 from table2 where table1.ID = table2.ID)
Is there any way to avoid having two nearly identical subqueries? This query is an obvious simplification but performance suffers and query is needlessly messy to read.
Unfortunately Informix don't support the FROM clause at UPDATE statement.
The way to workaround and you will get better results (performance) is change the UPDATE to MERGE statement.
This will work only if your database is version 11.50 or above
MERGE INTO table1 as t1
USING table2 as t2
ON t1.ID = t2.ID
WHEN MATCHED THEN UPDATE set (t1.col1, t1.col2) = (t2.col1, t2.col2);
Check IBM Informix manual for more information
Update with inner join can be used to avoid subqueries
something like this:
update t1
set col1 = t2.col1
from table1 t1
inner join table2 t2
on t1.ID = t2.ID
try this:
update table1 set col1 = (select col1 as newcol from table2 where table1.ID = table2.ID)
where exists (newcol)
Related
How can I update multiple rows in one query?
I have something like this:
update POL_VYMFOND set fk_vsoub='2245'
where fk_vsoub in (select HL_VYMSOUB.ID_VSOUB
from POL_VYMSEZN
inner join HL_VYMSEZN
on HL_VYMSEZN.ID_VSEZN=POL_VYMSEZN.FK_VSEZN
inner join HL_VYMSOUB
on HL_VYMSOUB.FK_VSEZN=HL_VYMSEZN.ID_VSEZN
where POL_VYMSEZN.FK_BUDOVA='4')
but definitely wrong.
Is it possible to do this?
I would like to change column values in one table according to values from another table.
Thank you
Generic answer for future developers.
SQL Server
UPDATE
t1
SET
t1.column = t2.column
FROM
Table1 t1
INNER JOIN Table2 t2
ON t1.id = t2.id;
Oracle (and SQL Server)
UPDATE
t1
SET
t1.colmun = t2.column
FROM
Table1 t1,
Table2 t2
WHERE
t1.ID = t2.ID;
MySQL
UPDATE
Table1 t1,
Table2 t2
SET
t1.column = t2.column
WHERE
t1.ID = t2.ID;
Firebird 2.1
UPDATE dest_table t1
SET
field1 = (select field1 from src_table t2 where t2.pk = t1.pk),
field2 = (select field2 from src_table t2 where t2.pk = t1.pk)
WHERE EXISTS (select 1 from src_table t2 where t2.pk = t1.pk)
For other versions of Firebird please check this link
Hope this will help you, and will solve your issue.
I have read lots of post about how to update multiple columns but still can't find right answer.
I have one table and I would like update this table from another table.
Update table1
set (a,b,c,d,e,f,g,h,i,j,k)=(t2.a,t2.b,t2.c,t2.d,t2.e,t2.f,t2.g,t2.h,t2.i,t2.j,t2.k)
from
(
SELECT ..... with join ... where ....
) t2
where table1.id=table2.id
If I running only select statement (between brackets) then script return values but not working with update
TSQL does not support row-value constructor. Use this instead:
UPDATE table1
SET a = t2.a,
b = t2.b,
(...)
FROM
(
SELECT ..... with join ... WHERE ....
) t2
WHERE table1.id = table2.id
You don't need to use a sub-query you can also simply do the following....
Update t1
set t1.a = t2.a
,t1.b = t2.b
,t1.c = t2.c
,t1.d = t2.d
.......
from table1 t1
JOIN table2 t2 ON t1.id = t2.id
WHERE .......
The above solution will work only for MSSQL.
In case of MySql you just need to first declare the tables
UPDATE
table1 t1 ,table2 t2
set
t1.field=t2.field
where
t1.id=t2.id;
In my case this worked..!!
The UPDATE SET commands implicitly apply on the table specified by , and it is not possible to specify the table on the SET operation.
Edit: Specify only the column name you want to update, do not mention the table.
I have an sql command similar to below one.
select * from table1
where table1.col1 in (select columnA from table2 where table2.keyColumn=3)
or table1.col2 in (select columnA from table2 where table2.keyColumn=3)
Its performance is really bad so how can I change this command? (pls note that the two sql commands in the paranthesis are exactly same.)
Try
select distinct t1.* from table1 t1
inner join table2 t2 ON t1.col1 =t2.columnA OR t1.col2 = t2.columnA
This is your query:
select *
from table1
where table1.col1 in (select columnA from table2 and t2.keyColumn = 3) or
table1.col2 in (select columnA from table2 and t2.keyColumn = 3);
Probably the best approach is to build an index on table2(keyColumn, columnA).
It is also possible that in has poor performance characteristics. So, you can try rewriting this as an exists query:
select *
from table1 t1
where exists (select 1 from table2 t2 where t2.columnA = t1.col1 and t2.keyColumn = 3) or
exists (select 1 from table2 t2 where t2.columnA = t2.col1 and t2.keyColumn = 3);
In this case, the appropriate index is table2(columnA, keyColumn).
Assuming you're doing this in VFP, use SYS(3054) to see how the query is being optimized and what part is not.
Are the main query and subqueries fully Rushmore-optimisable?
Since the subqueries do not appear to be correlated (i.e. they don't refer to table1 then as long as everything is fully supported by indexes you should be fine.
I have an update query like following:
update table TABLE1 set COL1 = 'X' where COL2 = 'Y' ---1
Support the values 'X' and 'Y' are fetched from database now TABLE2. E.g.
select COL1, COL2 from TABLE2. ----2
I want to update table TABLE1 with values from TABLE2.
Just to make it more clear, assume that TABLE2 has following values:
Can you please help me in doing this in a single query!
I am using Oracle 11g.
For Oracle, this is the most basic way to do it:
update TABLE1
set COL1 = (select TABLE2.COL1 from TABLE2 where TABLE2.COL2 = TABLE1.COL2)
where COL2 IN (select TABLE2.COL2 from TABLE2);
This can be inefficient in some cases since it could execute a subquery for every row in TABLE1.
Depending on the declaration of primary key or unique constraints on both tables, you may be able to use the updateable inline-view method, which is probably more efficient:
update
(select TABLE1.COL1 as T1C1, TABLE1.COL2 as T1C2, TABLE2.COL1 as T2C1
from TABLE1 join TABLE2 on TABLE2.COL2 = TABLE1.COL2
)
set T1C1 = T2C1;
#Dave Costa's answer is correct, if you limit yourself to update statements. However, I've found that using a merge statement in these situations allows me to do this in a more straightforward manner:
merge into TABLE1
using TABLE2
on (TABLE2.COL2 = TABLE1.COL2)
when matched then
update set TABLE1.COL1 = TABLE2.COL1;
update TABLE1
set TABLE1.COL1 = TABLE2.COL1
from TABLE1
join TABLE2 on TABLE1.COL2 = TABLE2.COL2
(this would work on Sql Server)
for oracle:
UPDATE Table1 t1
SET (X,Y) = (SELECT X,Y from Table2 WHERE ...YourConditions...)
WHERE ... Another Conditions ...
for mysql, sql-server
UPDATE t1
SET t1.X = t2, t2.Y = t2.Y
FROM Table1 t1, Table2 t2
WHERE t1.Something = t2.Something
I know its almost there, but base is telling me it can't find a column called table1.id when I know its there!
UPDATE table2 SET col1 = (SELECT field1 FROM table1 WHERE table2.id = table1.id) WHERE table1.id = table2.id
UPDATE table2 SET col1 = (SELECT field1 FROM table1 WHERE table2.id = table1.id)
table1 is unknown in the outer SQL.
What I get from your query, this will work
UPDATE table2 SET col1 = t1.field1
FROM table2 t2 INNER JOIN table1 t1 ON t2.id = t1.id
Instead of using a WHERE clause, try using an INNER JOIN clause. It is indeed late so forgive me for my code haha
UPDATE table2
SET col1 = (SELECT field1
FROM table1
WHERE table2.id = table1.id)
INNER JOIN table1
ON table2.id = table1.id
Option 1: No need to have outer WHERE clause.
Option 2: Do not use inner query unneccesarily. Use Inner Join instead