If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > UPDATE using a JOIN

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-29-06, 12:15
mattock mattock is offline
Registered User
 
Join Date: Aug 2006
Location: Leeds, UK
Posts: 104
UPDATE using a JOIN

Hi all

Whats the syntax for updating a table when using an inner join? I know how to use an INNER JOIN in a SELECT statement, and an UPDATE statement, but cant figure out how to put them together!

Eg, i want to update the transactions table with the customer id from the customers table by joining the transaction_id from both tables?

customers table
---------
cutomer_id - transaction_id
1 - 234
2 - 345
3 - 456


transactions table
---------
transaction_id - customer_id
234 - ?
345 - ?
456 - ?


thanks!

Last edited by mattock; 08-29-06 at 12:20.
Reply With Quote
  #2 (permalink)  
Old 08-29-06, 12:27
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
proper syntax may depend on which database system you're using (which you neglected to mention)

try this --
Code:
update transactions
   set customer_id = 
       ( select customer_id 
           from customers 
          where transaction_id = transactions.transaction_id )
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 08-29-06, 13:19
mattock mattock is offline
Registered User
 
Join Date: Aug 2006
Location: Leeds, UK
Posts: 104
i will be using it in both MSSQL and MYSQL, i was just after the basic syntax which you've given me of using an update with a join, didn't think about doing it the way you suggested (the simple way!)

All sorted now, thanks again rudy!
Reply With Quote
  #4 (permalink)  
Old 08-30-06, 06:58
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
Hi Mattock

This might be redundant as I know the overall goal of what you are attempting here but.... these two tables are not both part of your finalised, normalised database are they?
__________________
Testimonial:
Quote:
pootle flump
ur codings are working excelent.
Reply With Quote
  #5 (permalink)  
Old 08-30-06, 07:12
mattock mattock is offline
Registered User
 
Join Date: Aug 2006
Location: Leeds, UK
Posts: 104
hi flump

No, the two tables i gave were just an example i made up. They are similiar however to the tables i'm trying to change, but i only had one of the duplicate columns in each table to match one to the other, i will be deleting one of the columns once i have matched them together to make a one-to-many relationship - i realise you may think it was an odd example to to give as it had the same columns in each table.
Reply With Quote
  #6 (permalink)  
Old 08-30-06, 08:02
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
Super
__________________
Testimonial:
Quote:
pootle flump
ur codings are working excelent.
Reply With Quote
  #7 (permalink)  
Old 08-30-06, 08:18
mattock mattock is offline
Registered User
 
Join Date: Aug 2006
Location: Leeds, UK
Posts: 104
not bad for a newbie eh! ...geetting there.....slowly....
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On