jim.davidson Posted April 5, 2007 Share Posted April 5, 2007 I have a table with 3 fields field1 is an int field2 is an int field3 is an varchar I need to take the values from fields 1 and 2, combine them and put the result in field3. Example field1 = 4567 field2 = 2345 field3 = "45672345" How would I write the update command? Quote Link to comment https://forums.phpfreaks.com/topic/45762-solved-update-question/ Share on other sites More sharing options...
gluck Posted April 5, 2007 Share Posted April 5, 2007 use CONCAT to combine fields 1 and 2 and insert in 3. Quote Link to comment https://forums.phpfreaks.com/topic/45762-solved-update-question/#findComment-222279 Share on other sites More sharing options...
jim.davidson Posted April 5, 2007 Author Share Posted April 5, 2007 So something like this? update orders set field3 = concat(field1,field2) Quote Link to comment https://forums.phpfreaks.com/topic/45762-solved-update-question/#findComment-222281 Share on other sites More sharing options...
Wildbug Posted April 5, 2007 Share Posted April 5, 2007 Look in the MySQL manual, chapter 12 on functions, specifically the LPAD() and CONCAT() functions. UPDATE table_name SET field3=CONCAT(LPAD(field1,4,'0'),LPAD(field2,4,'0')); From your example I've assumed that you have four digit numbers and want an eight character string. If you want something different, you can probably find functions to fit your needs. Quote Link to comment https://forums.phpfreaks.com/topic/45762-solved-update-question/#findComment-222283 Share on other sites More sharing options...
jim.davidson Posted April 5, 2007 Author Share Posted April 5, 2007 Thank you one last question i only want to do this if field3 is empty so how to I tell if it's empty, is there a function for that? UPDATE table_name SET field3=CONCAT(LPAD(field1,4,'0'),LPAD(field2,4,'0')) where field3 empty; I new to mysql, but tryng Quote Link to comment https://forums.phpfreaks.com/topic/45762-solved-update-question/#findComment-222288 Share on other sites More sharing options...
gluck Posted April 5, 2007 Share Posted April 5, 2007 use WHERE field is null or field = '' depending on what you have in the DataBase. Quote Link to comment https://forums.phpfreaks.com/topic/45762-solved-update-question/#findComment-222301 Share on other sites More sharing options...
jim.davidson Posted April 5, 2007 Author Share Posted April 5, 2007 Thanks all, hopfully someday I can give answers instead of asking questions Quote Link to comment https://forums.phpfreaks.com/topic/45762-solved-update-question/#findComment-222333 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.