faqts : Computers : Databases : MySQL : Language and Syntax : Queries : Update

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

59 of 69 people (86%) answered Yes
Recently 7 of 10 people (70%) answered Yes

Entry

How can I concatenate the strings "Hello" + name, where name is a field, and put the result in the new field Hello_field ?

Nov 16th, 2001 21:17
Nick-faqts spam-Jenkins, Jean Tiberghien,


This is best answered by simply showing commands and result for the 
creation of a test table, adding some data, and then updating with a 
concatenation:


mysql> create database mysql_test;
Query OK, 1 row affected (0.06 sec)

mysql> use mysql_test;
Database changed
mysql> create table test (
    ->  name varchar(100),
    ->  hello_field varchar(100)
    -> );
Query OK, 0 rows affected (0.03 sec)

mysql> insert into test (name) values ("John"),("Peter"),("Mary"),
("Susan");
Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> select * from test;
+-------+-------------+
| name  | hello_field |
+-------+-------------+
| John  | NULL        |
| Peter | NULL        |
| Mary  | NULL        |
| Susan | NULL        |
+-------+-------------+
4 rows in set (0.00 sec)

mysql> update test set hello_field = concat("Hello ",name);
Query OK, 4 rows affected (0.02 sec)
Rows matched: 4  Changed: 4  Warnings: 0

mysql> select * from test;
+-------+-------------+
| name  | hello_field |
+-------+-------------+
| John  | Hello John  |
| Peter | Hello Peter |
| Mary  | Hello Mary  |
| Susan | Hello Susan |
+-------+-------------+
4 rows in set (0.00 sec)