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 > Database Server Software > MySQL > Update from inner join

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-19-11, 09:22
jx12345 jx12345 is offline
Registered User
 
Join Date: Apr 2010
Posts: 51
Update from inner join

I've been trying to do this update (which i'm sure should be really simple) but i just keep banging my head against the desk!!!

i'm writing a store locator and i have two tables:

table outcodes has the following fields:
outcode, lat, long

table stores has the following fields:
store_id, outcode, lat, long

all i need to do is to

update stores.lat = outcode.lat, stores.long=outcodes.long
where stores.outcode = outcodes.outcode.

Does that make sense?

I need to look up the latitude & longitude of a stores in the outcodes table using the stores outcode and update the latitude & longitude of the store accordingly...

any help much appreciated...

thanks
j
Reply With Quote
  #2 (permalink)  
Old 01-19-11, 09:33
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,535
Quote:
Originally Posted by jx12345 View Post
update stores.lat = outcode.lat, stores.long=outcodes.long
where stores.outcode = outcodes.outcode.
i'll walk you through it

first of all, please go to da manual and look up the UPDATE syntax

you will notice that the first two parameters after the word UPDATE are optional -- [LOW_PRIORITY] and [IGNORE]

after that comes something else, and what i'd like you to do is find out what it is, and try to fix your update statement accordingly

also, scroll down in that manual page to where they give an example of a joined update

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 01-19-11, 10:04
jx12345 jx12345 is offline
Registered User
 
Join Date: Apr 2010
Posts: 51
UPDATE stores, outcodes SET stores.lat = outcodes.lat
WHERE stores.outcode = outcodes.outcode



sorry i'm a complete chump...
Reply With Quote
  #4 (permalink)  
Old 01-19-11, 10:12
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,535
that's excellent, you're on the right track

now, what about the longitude... ?

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 01-19-11, 10:18
jx12345 jx12345 is offline
Registered User
 
Join Date: Apr 2010
Posts: 51
well actually i just cheated and changed the lat to long

UPDATE stores, outcodes SET stores.long= outcodes.long
WHERE stores.outcode = outcodes.outcode

if i wanted to do them all together could i write something like:

UPDATE stores, outcodes
SET stores.lat = outcodes.lat,
stores.long = outcodes.long
WHERE stores.outcode = outcodes.outcode
Reply With Quote
  #6 (permalink)  
Old 01-19-11, 10:39
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,535
nice job
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 01-19-11, 10:59
jx12345 jx12345 is offline
Registered User
 
Join Date: Apr 2010
Posts: 51
thanks again!
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