在MySQL中用空格连接两个字符串?
为此,您可以使用concat()
MySQL中的函数。让我们首先创建一个表-
mysql> create table DemoTable -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Name varchar(20), -> Subject varchar(100) -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable(Name,Subject) values('John','MySQL'); mysql> insert into DemoTable(Name,Subject) values('Chris','SQL Server'); mysql> insert into DemoTable(Name,Subject) values('Robert','MongoDB');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
输出结果
+----+--------+------------+ | Id | Name | Subject | +----+--------+------------+ | 1 | John | MySQL | | 2 | Chris | SQL Server | | 3 | Robert | MongoDB | +----+--------+------------+ 3 rows in set (0.00 sec)
这是用空格连接两个字符串的查询-
mysql> update DemoTable -> set Subject=concat(Name,' ',Subject); Rows matched: 3 Changed: 3 Warnings: 0
让我们检查表中的所有记录-
mysql> select *from DemoTable;
输出结果
+----+--------+------------------+ | Id | Name | Subject | +----+--------+------------------+ | 1 | John | John MySQL | | 2 | Chris | Chris SQL Server | | 3 | Robert | Robert MongoDB | +----+--------+------------------+ 3 rows in set (0.00 sec)