View Full Version : Concat 2 columns, save in 3rd column
Gary Williams
02-09-2008, 11:33 AM
Hi All,
Using SQLyog, I can concat 2 columns in my mysql database. Problem is, it is just the result (the concat'ed 2 columns) that is displayed. I need to concat 2 columns and save the result in another column in the same table.
What's the trick here?
Regards
Gary
Rohan_Shenoy
02-09-2008, 02:56 PM
Ok, I undestand that you want to do something like:
Col 1: Gary
Col 2: CodingForums.com
Col 3: Gary CodingForums.com
Just use the UPDATE query
1. Connect to MySQL and select database and table.
2. Use SELECT query to return values in col 1 an col 2 of a row.
3. Use them as variables and concatenate them.
4. UPDATE the database and fill th e 3rd column by the concatenated string.
StupidRalph
02-10-2008, 09:29 AM
Can I ask your reason for wanting to concat the two columns? Are you going to be getting rid of the two columns for the one? If not, you would be denormalizing your db by adding the third column.
I think you and I both need to ease up on using SQLyog and play with the command line for a couple weeks. :P I just asked the exact same question the other day. When basically all I had to do is use a composite primary key as Ralph_L_Mayo suggested.
Additionally, he suggested that I might benefit from using a view.
http://www.codingforums.com/showthread.php?t=132687
Gary Williams
02-15-2008, 11:49 AM
Hi Guys,
I made a simple script to test the changes would work on a sample of the table and yes, after combining the two columns I deleted the two original columns. Shyam gave me a simple solution that saved looping in asp.
The solution is in the post entitled "Modfying my postcode table".
Thanks for the help.
Regards
Gary
StupidRalph
02-15-2008, 01:12 PM
It could have been as simple as this in SQL:
UPDATE `table` SET `new_column` = CONCAT(`column1`,`column2`);
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.