在 MySQL 中用空格连接两个字符串?
mysqlmysqli database更新于 2025/8/23 2:52:17
为此,您可以使用 MySQL 中的 concat() 函数。让我们首先创建一个表 −
mysql> create table DemoTable -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Name varchar(20), -> Subject varchar(100) -> ); Query OK, 0 rows affected (17.73 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable(Name,Subject) values('John','MySQL');
Query OK, 1 row affected (1.19 sec)
mysql> insert into DemoTable(Name,Subject) values('Chris','SQL Server');
Query OK, 1 row affected (0.88 sec)
mysql> insert into DemoTable(Name,Subject) values('Robert','MongoDB');
Query OK, 1 row affected (2.62 sec)
使用 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); Query OK, 3 rows affected (0.38 sec) 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)
相关文章
有用资源
mysql 参考教程 - 该教程包含有关 mysql 的更多信息:https://www.cainiaomax.com/mysql/

