I have two tables in a PostgreSQL database; table1, table2. They both contain an id column. I want to add a column from table2, say col1, to table1 where table1.id = table2.id.
I am attempting to dot this via SQLalchemy. I keep getting the following error:
sqlalchemy.exc.ProgrammingError: (psycopg2.errors.SyntaxError) syntax error at or near "INNER"
Here is a snippet of the code:
engine = create_engine(...)
with engine.connect() as con:
con.execute("ALTER TABLE table1 ADD COLUMN col1 text")
con.execute("UPDATE table1 \
INNER JOIN table2 ON table1.id = table2.id \
SET table1.col1 = table2.col1")
Why am I getting this error?
PostgreSQL does not support UPDATE...JOIN like MySQL and MS Access. However, Postgres does support UPDATE...FROM even UPDATE...FROM...JOIN:
con.execute("""UPDATE table1 t1
SET col1 = t2.col1
FROM table2 t2
WHERE t1.id = t2.id""")
You cannot have the joins directly while updating in postgres and the way you have joined is incorrect. Please use below query,
update table1 set table1.col1 = qry.col1
from
(select col1 from table2) qry
where
table1.id = table2.qry;
That is not the correct syntax for update. Please see https://www.postgresql.org/docs/current/sql-update.html
update table1
set col1 = table2.col1
from table2
where table2.id = table1.id;
correct syntax for update with another table in postgresql :
UPDATE table1
set table1.col1 = table2.col1
FROM table2
where table1.id = table2.id
Related
I've go this request in SQL for a migration:
UPDATE table1
SET table1.value = table2.value
FROM table1
INNER JOIN table2
ON table2.othervalue2 = table1.key
WHERE table2.othervalue3 is NULL
Works well.
But not with Oracle. I know it isn't impossible to make join in a update with oracle, so I use this:
UPDATE table1 SET table1.value = (SELECT table2.value
FROM table2
WHERE table2.othervalue2 = table1.key
AND table2.othervalue3 is NULL)
WHERE EXISTS (SELECT table2.value
FROM table2
WHERE table2.othervalue2 = table1.key
AND table2.othervalue3 is NULL)
but I have an ORA-01427: Subquery returns more than one row.
Thanks
i would say you have inconsistent data. From the Errors you get i would say you have duplicates in the query specified.
SELECT table2.value
FROM table2
WHERE table2.othervalue2 = table1.key
AND table2.othervalue3 is NULL
you might wanne check it.or you need to have the data better specified you want to select for Update/Merge.
update t1
from
table1 t1,
table2 t2
set
t1.active_ind=0,
t1.row_status_cd='L'
where
t2.col1
is NULL
Getting "Column/ parameter table 1.t1 doesnt exists" error in Teradata SQL though those fields are available in t1 . can anybody help me to resolve this please?
As you only have to update table1 you don't need the t1 before the columnnames in tne SET, that is why you get the error.
but shouldn't there be a relation between table1 and table2, but as always you should test this first before using it, that wquery could have unforseen side effects
update t1
from
table1 t1,
table2 t2
set
active_ind=0,
row_status_cd='L'
where
t2.col1 is NULL
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)
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