blogfisher Posted March 15, 2009 Share Posted March 15, 2009 Hello, I am using php - mysql to update my database. I have two tables in a database. Both have same fields as below: Tbl1 & Tbl2: id, name, address, phone, pin, email I want to update row in Tbl1 where id = 3 with row in Tbl2 with id = 4. I am using below query, however its not working... UPDATE Tbl1 SET (id,name,address,phone, pin, email) WHERE id= 3 SELECT id,name,address,phone, pin, email FROM Tbl2 WHERE id = 4 Is this the correct way ? Quote Link to comment Share on other sites More sharing options...
AdRock Posted March 15, 2009 Share Posted March 15, 2009 Why don't you select what is in table 2 id 4 and put the result into variables then update the second table using those variables UPDATE tbl1 SET (id='$id', name='$name', address='$address', phone='$phone', pin='$pin', email='$email') WHERE id = 3 LIMIT 1 Quote Link to comment Share on other sites More sharing options...
blogfisher Posted March 15, 2009 Author Share Posted March 15, 2009 I can do that, however i want to know whether this is possible where i know only id and i want to update other fields automatically based on chosen id. Quote Link to comment Share on other sites More sharing options...
Stephen68 Posted March 15, 2009 Share Posted March 15, 2009 I found this I hope it will help you out some. http://publib.boulder.ibm.com/infocenter/iseries/v5r3/index.jsp?topic=/sqlp/rbafyupdatediftbl.htm I just did a google search for it Stephen Quote Link to comment Share on other sites More sharing options...
Mikedean Posted March 15, 2009 Share Posted March 15, 2009 This is completely untested and I don't even think it would work, but. UPDATE tb1 SET (name=(SELECT name FROM tbl2 WHERE id=4), address=(SELECT address FROM tbl2 WHERE id=4), phone=(SELECT phone FROM tbl2 WHERE id=4), pin=(SELECT pin FROM tbl2 WHERE id=4), email=(SELECT email FROM tbl2 WHERE id=4)) WHERE id = 3 LIMIT 1 I'm not sure that it will work, but that's the only way I can think of it working using a query. Edit: Eek, or just follow the link Stephen posted Quote Link to comment Share on other sites More sharing options...
blogfisher Posted March 15, 2009 Author Share Posted March 15, 2009 Hello Stephen , I am not sure how to do it. I tried but still giving error. That example updates entire table so no need to take reference of any id. I changed it in following ways. Each one is giving the same error and not working : 1. UPDATE Tbl1 SET ROW = (SELECT FROM Tbl2 WHERE id = 4) WHERE id = 3 2. UPDATE Tbl1 WHERE id = 3 SET ROW = (SELECT FROM Tbl2 WHERE id = 4) Any error in the statements? or they or not logical ? Quote Link to comment Share on other sites More sharing options...
Stephen68 Posted March 15, 2009 Share Posted March 15, 2009 Not really sure how about UPDATE tb1 SET ROW = (SELECT * FROM tb2 WHERE tb1.id = 4 AND tb2.id=4) Not sure if that would work or not, I'm not at home so I can not test it. Quote Link to comment Share on other sites More sharing options...
blogfisher Posted March 15, 2009 Author Share Posted March 15, 2009 Stephen, Thats not working.... looks like this is not possible or we dont have enough knowledge to know how to do that ... ;( Quote Link to comment Share on other sites More sharing options...
Stephen68 Posted March 15, 2009 Share Posted March 15, 2009 Sorry man, maybe you could try something like this. Might be a long query to type out depending on how many fields you have in your tables. UPDATE tb1, tb2 SET tb1.field = tb2.field, tb1.field2 = tb2.field2 WHERE tb1.id=tb2.id Just a thought Quote Link to comment Share on other sites More sharing options...
fesan Posted March 15, 2009 Share Posted March 15, 2009 Don't know if i understood this right, but cant u just use two separate mysql_queryes?? one that updates in tbl1 and one that updates tbl2.... Quote Link to comment Share on other sites More sharing options...
blogfisher Posted March 22, 2009 Author Share Posted March 22, 2009 Thanks for all your help.. finally i used simply traditional method ...updating table by simple UPDATE query by coping each field Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.