How to update PostgreSQL db with a triple join? - sql

I have three tables (a,b,c). I want to update a column in table a (named email) where
a.id = b.id, b.name = c.name.
This is what I have so far.
UPDATE a
SET email = "hi#gmail.com"
FROM a INNER JOIN b ON a.id = b.id
INNER JOIN c ON b.name = c.name
WHERE email IS NULL;
However I'm getting the error that table name a is specified more than once.
What can I do?

You don't specify the table being updated in the from clause. Use the where clause to correlated it:
UPDATE a
SET email = 'hi#gmail.com'
FROM b JOIN
c
ON b.name = c.name
WHERE a.id = b.id AND a.email IS NULL;

Related

Oracle to SQL Server conversion error

I have a particular condition in oracle which I am trying to run in MS SQL Server, but having issues..
Here is the code...
select a.*,b.*
from a
left join b on a.id = b.id
and (a.id,b.name) in
( select distinct id,name
from master where record = 'Active')
I am getting the error in line 3 where it says:
An expression of non-boolean type specified in a context where a
condition is expected, near ','.
Pls help
You need to rewrite that into an EXISTS condition:
select a.*,b.*
from a
left join b on a.id = b.id
and exists (select *
from master m
where m.record = 'Active'
and m.id = a.id
and m.name = b.name)
You can't write like this (a.id,b.name)
select a.*,b.*
from a
left join b on a.id = b.id
and a.id in
( select distinct id
from master where record = 'Active')
and b.name in ( select distinct name
from master where record = 'Active')

Sql Subquery result

I have 3 tables, A, B,C
Table A,Columns: latlong, name
Table B, columns : code, name
Table C, Columns : latlong, code
I want to update table A,column Name with the values from Table B, Column Name, something like:
update A set A.name =
(select b.name
from B where code = c.code)
where a.latlong = c.latlong
Note that all the columns are not related.
Would appreciate the right direction to go about it.
Have tried Sub queries with inner joins, but no good.
You can do this with an update using join:
update a
set name = b.name
from a join
c
on c.latlong = a.latlong join
b
on b.code = c.code;
Try update with INNER JOIN
update A set
A.name = B.name
FROM A
INNER JOIN C on C.latlong = A.latlong
INNER JOIN B on B.code = C.code
You have mentioned in your question the following:
I want to update table A,column Name with the values from Table B, Column Name
But what I can see from your query is that actually , you need only those values of the column Name of table B which has same value of code as in table C, and that your latlong in A should be the same as latlong in C, if I'm not mistaken.
Based on that, I can say you need an SQL JOIN operation for Tables B and C with table A. Something like this:
UPDATE A SET A.name = B.Name
FROM A
JOIN C
ON C.latlong = A.latlong
JOIN B
ON B.code = C.code
No need to create a SUBQUERY
Last condition is missing where Table A.Latlong = C.Latlong to pick up the correct code!

How make sql query "not in" more simply use only "join"?

Have query:
select a.id from selzde.elorder a
inner join selzde.elorder b on a.name = b.name
and a.workname = b.workname
and b.id = needId
where a.id not in (select id_elorder from selzde.drugselorder)
how make sql query not in more simply use only join?
select a.id from selzde.elorder a
inner join selzde.elorder b on a.name = b.name
and a.workname = b.workname
and b.id = needId
left outer join selzde.drugselorder d on a.id = d.id_elorder
where d.id_elorder is null
Left join to drugselorder, on id_elorder = a.id, then choose the ones with no match (i.e. where drugselorder is null)... I wouldn't say it was any more "simple", though.
One solution would be to use MINUS operator rather than not in as follows:
select a.id
from selzde.elorder a
inner join selzde.elorder b
on a.name = b.name
and a.workname = b.workname
and b.id = needId
MINUS
select id_elorder
from selzde.drugselorder
;
Hope this helps.
Regards,
Roger

How to update with inner join in Oracle

Could someone please verify whether inner join is valid with UPDATE statment in PL SQL?
e.g.
Update table t
set t.value='value'
from tableb b inner join
on t.id=b.id
inner join tablec c on
c.id=b.id
inner join tabled d on
d.id=c.id
where d.key=1
This synthax won't work in Oracle SQL.
In Oracle you can update a join if the tables are "key-preserved", ie:
UPDATE (SELECT a.val_a, b.val_b
FROM table a
JOIN table b ON a.b_pk = b.b_pk)
SET val_a = val_b
Assuming that b_pk is the primary key of b, here the join is updateable because for each row of A there is at most one row from B, therefore the update is deterministic.
In your case since the updated value doesn't depend upon another table you could use a simple update with an EXIST condition, something like this:
UPDATE mytable t
SET t.VALUE = 'value'
WHERE EXISTS
(SELECT NULL
FROM tableb b
INNER JOIN tablec c ON c.id = b.id
INNER JOIN tabled d ON d.id = c.id
WHERE t.id = b.id
AND d.key = 1)
update t T
set T.value = 'value'
where T.id in (select id from t T2, b B, c C, d D
where T2.id=B.id and B.id=C.id and C.id=D.id and D.key=1)
-- t is the table name, T is the variable used to reffer to this table

Update with two tables?

I am trying to update table A with data from table B. I thought I could do
something like:
UPDATE A
SET A.name = B.name
WHERE A.id = B.id
but alas, this does not work.
Anyone have an idea of how I can do this?
Your query does not work because you have no FROM clause that specifies the tables you are aliasing via A/B.
Please try using the following:
UPDATE A
SET A.NAME = B.NAME
FROM TableNameA A, TableNameB B
WHERE A.ID = B.ID
Personally I prefer to use more explicit join syntax for clarity i.e.
UPDATE A
SET A.NAME = B.NAME
FROM TableNameA A
INNER JOIN TableName B ON
A.ID = B.ID
For Microsoft Access
UPDATE TableA A
INNER JOIN TableB B
ON A.ID = B.ID
SET A.Name = B.Name
I was scratching my head, not being able to get John Sansom's Join syntax work, at least in MySQL 5.5.30 InnoDB.
It turns out that this doesn't work.
UPDATE A
SET A.x = 1
FROM A INNER JOIN B
ON A.name = B.name
WHERE A.x <> B.x
But this works:
UPDATE A INNER JOIN B
ON A.name = B.name
SET A.x = 1
WHERE A.x <> B.x
The answers didn't work for me with postgresql 9.1+
This is what I had to do (you can check more in the manual here)
UPDATE schema.TableA as A
SET "columnA" = "B"."columnB"
FROM schema.TableB as B
WHERE A.id = B.id;
You can omit the schema, if you are using the default schema for both tables.