在 MYSQL 中将字符串存储在变量中,并将其连接起来,以在一个单列中显示它们
为此,在 MySQL 中使用 CONCAT_WS()。我们首先创建一个 -
mysql> create table DemoTable1433 -> ( -> ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> ClientFirstName varchar(20), -> ClientLastName varchar(20) -> ); Query OK, 0 rows affected (0.58 sec)
使用插入向表中插入一些记录 -
mysql> insert into DemoTable1433(ClientFirstName,ClientLastName) values('David','Miller'); Query OK, 1 row affected (0.14 sec)
使用选择从表中显示所有记录 -
mysql> select * from DemoTable1433;
这将产生以下输出 -
+----------+-----------------+----------------+ | ClientId | ClientFirstName | ClientLastName | +----------+-----------------+----------------+ | 1 | David | Miller | +----------+-----------------+----------------+ 1 row in set (0.00 sec)
以下是对变量进行字符串存储并进行连接的查询 -
mysql> set @concat_variable:=(select concat_ws('-',ClientFirstName,ClientLastName) from DemoTable1433); Query OK, 0 rows affected (0.00 sec)
mysql> select @concat_variable;
这将产生以下输出 -
+------------------+ | @concat_variable | +------------------+ | David-Miller | +------------------+ 1 row in set (0.00 sec)
广告