473,378 Members | 1,309 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

UPDATE and outer joins

I wonder if anybody could give me a few pointers on a problem I face.

I need to do an UPDATE on table A, from an effective left outer join
on A and another table B. (This is trying to perform a number translation,
where the items that need it are rare.)

The following points *I think* are relevant:

1) The FROM clause in UPDATE should *only* show additional tables,
otherwise I'll get an extra join I didn't want! (IMHO this could do
with being emphasised in the docs.)

2) If a FROM clause is present, but no WHERE clause, a cross join is performed.

3) It is possible to 'emulate' an inner join with statements in a WHERE
clause, but is *not* possible to do so for an outer join. (4.2.2 in the
User's Guide)

If I have the above correct then it seems that there is no way to do
an UPDATE in the way I want in a single statement? I am currently
doing a left outer join into a temporary file, then the UPDATE, but this
involves two joins!

Is there a better way of doing this, or do I have to keep using the
temporary file?

Regards,
Harry.
---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 12 '05 #1
5 15150
On Wed, Oct 08, 2003 at 12:23:04 +0100,
Harry Broomhall <ha*************@uk.easynet.net> wrote:
I wonder if anybody could give me a few pointers on a problem I face.

I need to do an UPDATE on table A, from an effective left outer join
on A and another table B. (This is trying to perform a number translation,
where the items that need it are rare.)

The following points *I think* are relevant:

1) The FROM clause in UPDATE should *only* show additional tables,
otherwise I'll get an extra join I didn't want! (IMHO this could do
with being emphasised in the docs.)


But that might be the best approach. If you do a left join of A with B in
the where clause and then an inner join of that result with A you should
get what you want. If the optimizer does a good job, it may not even be
much of a hit to do that.

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 12 '05 #2
On Wed, 8 Oct 2003 12:23:04 +0100 (BST), Harry Broomhall
<ha*************@uk.easynet.net> wrote:
I am currently
doing a left outer join into a temporary file, then the UPDATE, but this
involves two joins!


UPDATE a
SET col1 = b.col11, col2 = b.col12
FROM a AS x LEFT JOIN b ON (x.??? = b.???)
WHERE a.pk = x.pk;

might not run faster, but at least it is only one statement and you
don't have to care for temporary tables ...

Servus
Manfred

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 12 '05 #3
Bruno Wolff III writes:
On Wed, Oct 08, 2003 at 12:23:04 +0100,
Harry Broomhall <ha*************@uk.easynet.net> wrote:
I wonder if anybody could give me a few pointers on a problem I face.

I need to do an UPDATE on table A, from an effective left outer join
on A and another table B. (This is trying to perform a number translation,
where the items that need it are rare.)

The following points *I think* are relevant:

1) The FROM clause in UPDATE should *only* show additional tables,
otherwise I'll get an extra join I didn't want! (IMHO this could do
with being emphasised in the docs.)


But that might be the best approach. If you do a left join of A with B in
the where clause and then an inner join of that result with A you should
get what you want. If the optimizer does a good job, it may not even be
much of a hit to do that.


Er - I though that was one of the points I made - you can't get a
left join in a WHERE clause? If I am wrong about that then could you
indicate how I might do it?

I presumed that the left join would have to be in the FROM clause, i.e.:

UPDATE A set cli = num FROM A left join B on (details) WHERE (etc)

I tried this approach early on, and now I think about it I realize I
didn't have a WHERE clause - which would have done a cross join which would
have taken forever!

Regards,
Harry.
---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 12 '05 #4
On Wed, Oct 08, 2003 at 15:40:13 +0100,
Harry Broomhall <ha*************@uk.easynet.net> wrote:
Bruno Wolff III writes:
On Wed, Oct 08, 2003 at 12:23:04 +0100,
Harry Broomhall <ha*************@uk.easynet.net> wrote:
I wonder if anybody could give me a few pointers on a problem I face.

I need to do an UPDATE on table A, from an effective left outer join
on A and another table B. (This is trying to perform a number translation,
where the items that need it are rare.)

The following points *I think* are relevant:

1) The FROM clause in UPDATE should *only* show additional tables,
otherwise I'll get an extra join I didn't want! (IMHO this could do
with being emphasised in the docs.)
But that might be the best approach. If you do a left join of A with B in
the where clause and then an inner join of that result with A you should
get what you want. If the optimizer does a good job, it may not even be
much of a hit to do that.


Er - I though that was one of the points I made - you can't get a
left join in a WHERE clause? If I am wrong about that then could you
indicate how I might do it?


I slipped on that. I did mean that you could do left join in the from item
list and then join that to the table be updated by using an appropiate
where condition.

I presumed that the left join would have to be in the FROM clause, i.e.:

UPDATE A set cli = num FROM A left join B on (details) WHERE (etc)

I tried this approach early on, and now I think about it I realize I
didn't have a WHERE clause - which would have done a cross join which would
have taken forever!


Someone else responded with the same suggestion, but a bit more fleshed out.

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 12 '05 #5
Manfred Koizar writes:
On Wed, 8 Oct 2003 12:23:04 +0100 (BST), Harry Broomhall
<ha*************@uk.easynet.net> wrote:
I am currently
doing a left outer join into a temporary file, then the UPDATE, but this
involves two joins!


UPDATE a
SET col1 = b.col11, col2 = b.col12
FROM a AS x LEFT JOIN b ON (x.??? = b.???)
WHERE a.pk = x.pk;

might not run faster, but at least it is only one statement and you
don't have to care for temporary tables ...

Many thanks to all who made suggestions. The above gave me enough
clues to get a working UPDATE query working.

It is a shame it can't be done in a single join though!

Regards,
Harry.

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 12 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dennis Hancy | last post by:
I am trying to create an outer join between two tables in a query that includes several other tables. When I double-click on the Join line, it presents three join options: 1) ONLY records from...
7
by: Steve | last post by:
I have a SQL query I'm invoking via VB6 & ADO 2.8, that requires three "Left Outer Joins" in order to return every transaction for a specific set of criteria. Using three "Left Outer Joins"...
1
by: Frank via DBMonster.com | last post by:
Hello, My ODBC driver from DB2 connect does not allow outer joins. Is this normal? Does anyone know how to configure it so it allows outer joins? Greetings, Frank
1
by: Lumpierbritches | last post by:
I'm trying to pull all the parents of a particular animal and I have my SQL statement that says not supported and when I attempt to run the Query, I get The SQL statement couldn't be executed...
4
by: Brian Parker | last post by:
I'm new to ms-sqlserver ( 2000 ) and need to get an OUTER JOIN working on a three table query. Assumptions: -- I have events in the Event table. -- Each event CAN have one Transaction, but it's...
0
by: daniel.stahr | last post by:
Hi, I figured out that MQT's can not perform outer joins if you choose refresh immediate. Does anybody know if this feature is planned for any future fixpacks or versions? If not, any ideas for...
1
by: mkepick | last post by:
migrated databases from sybase to mssql, migration exported sybase written queries written with the application build in query tool and imported to mssql databases. all db and tables migrated...
2
by: Michael Scott | last post by:
This is driving me nuts. Can anyone help? I have a program which searches for and analyses Access databases across a corporate network. At one point in the code I am looping quickly through all...
3
by: rmotwani | last post by:
Hi, Everybody ! I have a small problem. If I run a query with group by having two outer joins, there, group by is not working i suppose. Case: table 1 : Product master having product...
7
by: Tania Louie | last post by:
Hey All, I really need some help here! I currently have 3 databases that I am writing a script for to extract data. I need to join these databases together to get one extract, but I have a...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.