Stumbling with a sql insert

kupe

Registered User.
Local time
Today, 17:45
Joined
Jan 16, 2003
Messages
462
Two tables – tblOne and tblTwo have identical fields. tblOne has fuller records than tblTwo.

I want to say in sql (MySql to be specific)

INSERT INTO tblTwo (Address, City, etc)
SELECT Address, City, etc
FROM tblOne
WHERE tblTwo.Sname = tblOne.Sname;

WHERE-wise, this isn’t the way, I now know. But I can’t find any examples of how to. Be very grateful for advice, experts.
 
I think, instead of "INSERT INTO" statement you should use "UPDATE".

Try the following:

UPDATE tblTwo INNER JOIN tblOne ON tblTwo.Sname = tblOne.Sname
SET tblTwo.Address = tblOne.Address ,
tblTwo.City = tblOne.City,
tblTwo.etc = tblOne.etc;

Over.
 
Thanks, Lobodava, I will give it a try tonight.
 
Lobo

You're a star. Thank you very much. Works very well.

I don't have left joins/right joins et al at all clearly in my mind. Is there a simple rule about which decides which, please?
 
:rolleyes:


Regarding the joins... it is simple.

ALL records will be selected from a table which is named in JOIN type.

tblOne LEFT JOIN tblTwo - all records from tblOne (tblOne in LEFT position in this string)
tblTwo LEFT JOIN tblOne - all records from tblTwo (tblTwo in LEFT position in this string)
tblOne RIGHT JOIN tblTwo - all records from tblTwo (tblTwo in RIGHT position in this string)
tblTwo RIGHT JOIN tblOne - all records from tblOne (tblOne in RIGHT position in this string)
 
Many thanks. Looks simple put like that. I'll be referring to this advice often. Very grateful.
 

Users who are viewing this thread

Back
Top Bottom