在结果表中显示 MySQL 用户自定义变量值?
对变量使用 @ 并使用 concat_ws() 在表中显示连接结果。我们首先创建一个表 −
mysql> create table DemoTable1508 -> ( -> StudentFirstName varchar(20), -> StudentLastName varchar(20) -> ); Query OK, 0 rows affected (0.55 sec)
使用插入命令向表中插入一些记录 −
mysql> insert into DemoTable1508 values('Chris','Brown'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1508 values('David','Miller'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1508 values('John','Doe'); Query OK, 1 row affected (0.15 sec)
使用 select 语句显示表中的所有记录 −
mysql> select * from DemoTable1508;
这将产生以下输出 −
+------------------+-----------------+ | StudentFirstName | StudentLastName | +------------------+-----------------+ | Chris | Brown | | David | Miller | | John | Doe | +------------------+-----------------+ 3 rows in set (0.00 sec)
以下是对结果表显示 MySQL 变量的查询 −
mysql> select @FullName:=concat_ws('-',StudentFirstName,StudentLastName) from DemoTable1508;
这将产生以下输出 −
+------------------------------------------------------------+ | @FullName:=concat_ws('-',StudentFirstName,StudentLastName) | +------------------------------------------------------------+ | Chris-Brown | | David-Miller | | John-Doe | +------------------------------------------------------------+ 3 rows in set (0.03 sec)
广告