Wednesday, December 26, 2012

CONCAT in MySQL


CONCAT function returns the string that results from concatenating the arguments.

Example 1:
SELECT CONCAT(column1,column2) from table;
The output for the above will be
Mohanlal
SureshGopi

In between the two columns if you want space or comma, rewrite the query as below

SELECT CONCAT(column1,',',column2) from table;
The output for the above will be
Mohan,lal
Suresh,Gopi

We can also update the table using CONCAT function.

Example 2:
UPDATE table SET column1=CONCAT(column1,column2)

Example 3:
UPDATE table SET column1=CONCAT(column1,'some string')

Example 4: using PHP
mysql_query("UPDATE table SET column1=CONCAT(column1,' ".$variable." ')");

No comments:

Post a Comment