MySQL 查询来在单独一列中将列名显示为名和姓完整的姓名?
为此,在 MySQL 中使用 CONCAT() 方法。我们先创建一个表 -
mysql> create table DemoTable ( FirstName varchar(50), LastName varchar(50) ); Query OK, 0 rows affected (0.63 sec)
使用 insert 命令在表中插入一些记录 -
mysql> insert into DemoTable values('Chris','Brown'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values('David','Miller'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('John','Doe'); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable values('Adam','Smith'); Query OK, 1 row affected (0.25 sec)
使用 select 语句从表中显示所有记录 -
mysql> select *from DemoTable;
这将产生以下输出 -
+-----------+----------+ | FirstName | LastName | +-----------+----------+ | Chris | Brown | | David | Miller | | John | Doe | | Adam | Smith | +-----------+----------+ 4 rows in set (0.00 sec)
以下是将名和姓连接成全名的查询 -
mysql> select concat(FirstName,' ',LastName) AS FullName from DemoTable;
这将产生以下输出 -
+--------------+ | FullName | +--------------+ | Chris Brown | | David Miller | | John Doe | | Adam Smith | +--------------+ 4 rows in set (0.00 sec)
广告