Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/45762-solved-update-question/
Share on other sites

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.

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.