suprsnipes Posted May 30, 2009 Share Posted May 30, 2009 In basic terms I want to UPDATE table 1 column 2 with the data from table 2 column 2 WHERE table 1 column 1 = table 2 column 1 After spending 2 days on this and after trying all various methods I'm stumped., it just won't work. table 1 = t1 table 2 = t2 t1 column 1 = id t1 column 2 = type t2 column 1 = id t2 column 2 = type Can anyone help me? Quote Link to comment https://forums.phpfreaks.com/topic/160243-sql-query-please-help-me-i-have-spent-2-days-on-this-with-no-progress/ Share on other sites More sharing options...
Michdd Posted May 30, 2009 Share Posted May 30, 2009 $result = mysql_query('Select type FROM `t2` WHERE id="' . $id . '" LIMIT 1'); $row = mysql_fetch_assoc($result); mysql_query('UPDATE `t1` SET type="' . $row['type'] . '" WHERE id="' . $id . '"'); Quote Link to comment https://forums.phpfreaks.com/topic/160243-sql-query-please-help-me-i-have-spent-2-days-on-this-with-no-progress/#findComment-845574 Share on other sites More sharing options...
suprsnipes Posted May 30, 2009 Author Share Posted May 30, 2009 Thanks for the reply Michdd. Sorry my definitions of the columns on t2 were slightly incorrect could you please update your suggested code for me. table 1 = t1 table 2 = t2 t1 column 1 = id t1 column 2 = type t2 column 1 = id1 t2 column 2 = type1 Quote Link to comment https://forums.phpfreaks.com/topic/160243-sql-query-please-help-me-i-have-spent-2-days-on-this-with-no-progress/#findComment-845585 Share on other sites More sharing options...
roopurt18 Posted May 30, 2009 Share Posted May 30, 2009 http://dev.mysql.com/doc/refman/5.0/en/update.html update t1, t2 set t1.type=t2.type1 where t1.id=t2.id1 Quote Link to comment https://forums.phpfreaks.com/topic/160243-sql-query-please-help-me-i-have-spent-2-days-on-this-with-no-progress/#findComment-845593 Share on other sites More sharing options...
suprsnipes Posted May 30, 2009 Author Share Posted May 30, 2009 I'm getting an notice: Undefined variable:id on line 9 and line 11. 9. $result = mysql_query('Select `type` FROM `t2` WHERE `id`="'. $id . '" LIMIT 1'); 10. $row = mysql_fetch_assoc($result); 11. mysql_query('UPDATE `t1` SET type="' . $row['type'] . '" WHERE id="' . $id . '"'); Quote Link to comment https://forums.phpfreaks.com/topic/160243-sql-query-please-help-me-i-have-spent-2-days-on-this-with-no-progress/#findComment-845613 Share on other sites More sharing options...
Ken2k7 Posted May 30, 2009 Share Posted May 30, 2009 Well you haven't given us any code to work with so Michdd put up an example. You have to define the variable $id if you actually need that WHERE clause case. Quote Link to comment https://forums.phpfreaks.com/topic/160243-sql-query-please-help-me-i-have-spent-2-days-on-this-with-no-progress/#findComment-845899 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.