dbTalk Databases Forums  

how to UPDATE based on a SELECT

comp.databases.mysql comp.databases.mysql


Discuss how to UPDATE based on a SELECT in the comp.databases.mysql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Nick Wedd
 
Posts: n/a

Default how to UPDATE based on a SELECT - 10-14-2010 , 02:23 PM






I have a large (9000 records) table 'map', with a key field 'id', and a
data field 'p'.

I have a small (30 records) table 'hexads', with a unique field 'rm0',
whose value matches one of the 'id' values in 'map', and a currently
empty field 'p03' which I want to put data in.

What I want to do is
for each record of 'hexads'
look up its 'rm0' value in the 'id' field of 'map'
take the 'p' value from that record of 'map'
and put it in the 'p03' field of the record of 'hexads'

How can I do this in MySQL?

Nick
--
Nick Wedd nick (AT) maproom (DOT) co.uk

Reply With Quote
  #2  
Old   
Luuk
 
Posts: n/a

Default Re: how to UPDATE based on a SELECT - 10-14-2010 , 02:44 PM






On 14-10-10 21:23, Nick Wedd wrote:
Quote:
I have a large (9000 records) table 'map', with a key field 'id', and a
data field 'p'.

I have a small (30 records) table 'hexads', with a unique field 'rm0',
whose value matches one of the 'id' values in 'map', and a currently
empty field 'p03' which I want to put data in.

What I want to do is
for each record of 'hexads'
look up its 'rm0' value in the 'id' field of 'map'
take the 'p' value from that record of 'map'
and put it in the 'p03' field of the record of 'hexads'

How can I do this in MySQL?

Nick

update hexads H
set H.p03=(SELECT M.p
from map M
where M.id=H.rm0 LIMIT 1)


But a better answer might be found at:
http://dev.mysql.com/doc/refman/5.1/en/update.html

--
Luuk

Reply With Quote
  #3  
Old   
Nick Wedd
 
Posts: n/a

Default Re: how to UPDATE based on a SELECT - 10-14-2010 , 03:02 PM



In message <s43ko7-akb.ln1 (AT) qqqqq (DOT) xs4all.nl>, Luuk <Luuk (AT) invalid (DOT) lan>
writes
Quote:
On 14-10-10 21:23, Nick Wedd wrote:
I have a large (9000 records) table 'map', with a key field 'id', and a
data field 'p'.

I have a small (30 records) table 'hexads', with a unique field 'rm0',
whose value matches one of the 'id' values in 'map', and a currently
empty field 'p03' which I want to put data in.

What I want to do is
for each record of 'hexads'
look up its 'rm0' value in the 'id' field of 'map'
take the 'p' value from that record of 'map'
and put it in the 'p03' field of the record of 'hexads'

How can I do this in MySQL?

Nick


update hexads H
set H.p03=(SELECT M.p
from map M
where M.id=H.rm0 LIMIT 1)
It works! Many thanks for a very fast correct answer.

Nick

Quote:
But a better answer might be found at:
http://dev.mysql.com/doc/refman/5.1/en/update.html
I'll have a look.
--
Nick Wedd nick (AT) maproom (DOT) co.uk

Reply With Quote
  #4  
Old   
Doug Miller
 
Posts: n/a

Default Re: how to UPDATE based on a SELECT - 10-14-2010 , 07:56 PM



In article <fUnU4CCLj1tMFAh+@maproom.demon.co.uk>, Nick Wedd <nick (AT) maproom (DOT) co.uk> wrote:
Quote:
I have a large (9000 records) table 'map', with a key field 'id', and a
data field 'p'.

I have a small (30 records) table 'hexads', with a unique field 'rm0',
whose value matches one of the 'id' values in 'map', and a currently
empty field 'p03' which I want to put data in.

What I want to do is
for each record of 'hexads'
look up its 'rm0' value in the 'id' field of 'map'
take the 'p' value from that record of 'map'
and put it in the 'p03' field of the record of 'hexads'

How can I do this in MySQL?
The following would work in Access; I don't know if MySQL supports the same
syntax:

UPDATE hexads INNER JOIN map ON map.id = hexads.rm0
SET hexads.p03 = map.p
;

Reply With Quote
  #5  
Old   
Lennart Jonsson
 
Posts: n/a

Default Re: how to UPDATE based on a SELECT - 10-14-2010 , 08:08 PM



On 2010-10-14 21:23, Nick Wedd wrote:
Quote:
I have a large (9000 records) table 'map', with a key field 'id', and a
data field 'p'.

I have a small (30 records) table 'hexads', with a unique field 'rm0',
whose value matches one of the 'id' values in 'map', and a currently
empty field 'p03' which I want to put data in.

Why do you need to duplicate the information?

/Lennart

[...]

Reply With Quote
  #6  
Old   
Luuk
 
Posts: n/a

Default Re: how to UPDATE based on a SELECT - 10-15-2010 , 02:30 AM



On 15-10-10 02:56, Doug Miller wrote:
Quote:
In article<fUnU4CCLj1tMFAh+@maproom.demon.co.uk>, Nick Wedd<nick (AT) maproom (DOT) co.uk> wrote:
I have a large (9000 records) table 'map', with a key field 'id', and a
data field 'p'.

I have a small (30 records) table 'hexads', with a unique field 'rm0',
whose value matches one of the 'id' values in 'map', and a currently
empty field 'p03' which I want to put data in.

What I want to do is
for each record of 'hexads'
look up its 'rm0' value in the 'id' field of 'map'
take the 'p' value from that record of 'map'
and put it in the 'p03' field of the record of 'hexads'

How can I do this in MySQL?

The following would work in Access; I don't know if MySQL supports the same
syntax:

UPDATE hexads INNER JOIN map ON map.id = hexads.rm0
SET hexads.p03 = map.p
;
Now i know why i was pointing Nick toward the docs....
This UPDATE <table> INNER JOIN ....
is so much nicer then my solution

;-)

--
Luuk

Reply With Quote
Reply




Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.