Update with two tables? - sql

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.

Related

How to update PostgreSQL db with a triple join?

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;

Join WHERE clause - table has row with defined value, or no entry

Can anyone help me figure out the correct WHERE clause for the following scenario:
select A.name
from tableA A, tableB B
where A.id = B.id
and
(
B.field = 5
OR
B.hasNoRowForJoinedID
)
I feel like I'm missing something really obvious here in how to accomplish this, but I can't for the life of me put my finger on it.
You are using an outdated SQL Syntax. To perform the LEFT OUTER JOIN based your your request above, you can do the following:
SELECT A.name
FROM A
LEFT OUTER JOIN B ON A.id = B.id
WHERE (B.field = 5 OR B.field IS NULL)
Use proper join syntax and not the outdated ones:
select A.name
from tableA A
left join tableB B on A.id = B.id and B.field = 5

Remove Duplicate Code from UPDATE

I have the following query.
UPDATE A SET b = (SELECT b FROM B WHERE A.a_id = B.a_id AND B.value = ?)
This might fill A with NULL values if no a_id exists in B where value = ?. but this is okay, because before executing this query, it is certain that A.b contains only NULL values to begin with.
However, I need the number of updated columns to reflect the number of updates performed. So I changed it into this:
UPDATE A SET b = (SELECT b FROM B WHERE A.a_id = B.a_id AND B.value = ?)
WHERE EXISTS (SELECT b FROM B WHERE A.a_id = B.a_id AND B.value = ?)
I don't like this solution, because now I have duplicate code and have to fill the parameter multiple times. This gets even uglier when the where clause gets more complicated.
Is there a way to get rid of this duplicate code?
(BTW I'm on Oracle 10, but I prefer DB independent solutions)
Updat using an inner join
UPDATE A
INNER JOIN B ON A.a_id = B.a_id
SET A.b = B.b
WHERE B.value = ?
If that isn't allowed with your particular RDBMS, perhaps you could SELECT the old and new values into an aliased table expression and update using that. See Update statement with inner join on Oracle

Ambiguous Column Reference with an AS alias

I am unsure as to how to resolve an ambiguous column reference when using an alias.
Imagine two tables, a and b that both have a name column. If I join these two tables and alias the result, I do not know how to reference the name column for both tables. I've tried out a few variants, but none of them work:
Attempt 1
SELECT a.name, b.name
FROM (a INNER JOIN b ON a.id = b.id) AS x
This doesn't work as a and b are out of scope.
Attempt 2
SELECT x.a.name, x.b.name
FROM (a INNER JOIN b ON a.id = b.id) AS x
SQL syntax doesn't work like that.
Attempt 3
SELECT x.name, x.name
FROM (a INNER JOIN b ON a.id = b.id) AS x
That's just plain ambiguous!
I'm all out of ideas - any help would be much appreciated.
don't enclose it with parenthesis since (a INNER JOIN b ON a.id = b.id) is not a complete query.
SELECT a.name AS A_Name,
b.name AS B_Name
FROM a INNER JOIN b
ON a.id = b.id
or (assuming) if you have longer tables names and you want to make it short,
SELECT a.name AS A_Name,
b.name AS B_Name
FROM longTableNameA a
INNER JOIN longTableNameB b
ON a.id = b.id

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