Selecting from one table to UPDATE another.

2002-07-25 Thread Simon Green

What I am trying to do in MySQL.

Table1

UserID  ProductID   Old_UserID
1   2   4
2   3   5   

Table2
UserID ProductID
4   Null
5   Null

Table1 has new correct data. 
Table2 has old incorrect data.
The link is Table2.UserID and Old_UserID

What I need to do is update Table 2 with the new data from Table 1,
But I can not get this to work..e.g.

UPDATE Table2.ProductID
SET Table2.ProductID = Table1.ProductID
WHERE Table1.Old_UserID = Table2.UserID

REPLACE will not work as these are not KEY fields.
Once I have done the ProductID I should then be able to do the UserID using
the ProductID (as all values in one table are unique).

Help
Simon

-
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/   (the list archive)

To request this thread, e-mail [EMAIL PROTECTED]
To unsubscribe, e-mail [EMAIL PROTECTED]
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php




Re: Selecting from one table to UPDATE another.

2002-07-25 Thread Ralf Narozny

Hello!

Simon Green wrote:

What I am trying to do in MySQL.

Table1

UserID ProductID   Old_UserID
1  2   4
2  3   5   

Table2
UserID ProductID
4  Null
5  Null

Table1 has new correct data. 
Table2 has old incorrect data.
The link is Table2.UserID and Old_UserID

What I need to do is update Table 2 with the new data from Table 1,
But I can not get this to work..e.g.

UPDATE Table2.ProductID
SET Table2.ProductID = Table1.ProductID
WHERE Table1.Old_UserID = Table2.UserID

REPLACE will not work as these are not KEY fields.
Once I have done the ProductID I should then be able to do the UserID using
the ProductID (as all values in one table are unique).
  


SELECT
concat('UPDATE Table2 SET ProductID = ',
t1.ProductID,
' WHERE UserID = ',
t1.UserID,
';'
)
INTO OUTFILE
'/tmp/update.sql'
FROM
Table1 t1
LEFT JOIN Table2 t2 ON (t1.Old_UserID = t2.UserID)
WHERE
t2.UserID IS NOT NULL
;

\. /tmp/update.sql

This might help...but better take a look at /tmp/update.sql, before 
executing it!!!

Greetings
 Ralf

-- 
Ralf Narozny
SPLENDID Internet GmbH  Co KG
Skandinaviendamm 212, 24109 Kiel, Germany
fon: +49 431 660 97 0, fax: +49 431 660 97 20
mailto:[EMAIL PROTECTED], http://www.splendid.de




-
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/   (the list archive)

To request this thread, e-mail [EMAIL PROTECTED]
To unsubscribe, e-mail [EMAIL PROTECTED]
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php