MySQL中如何选择多个列并显示在单个列中
为此使用concat()。让我们首先创建一个表-
create table DemoTable -> ( -> FirstName varchar(30), -> LastName varchar(30) -> );
使用插入命令在表中插入一些记录-
insert into DemoTable values('Chris','Brown'); insert into DemoTable values('Adam','Smith'); insert into DemoTable values('Carol','Taylor');
使用select语句显示表中的所有记录-
select *from DemoTable;
输出结果
这将产生以下输出-
+-----------+----------+ | FirstName | LastName | +-----------+----------+ | Chris | Brown | | Adam | Smith | | Carol | Taylor | +-----------+----------+ 3 rows in set (0.00 sec)
以下是选择具有单个别名的多列的查询-
select concat(FirstName,' ',LastName) as concatValue from DemoTable order by concatValue DESC;
输出结果
这将产生以下输出-
+--------------+ | concatValue | +--------------+ | Chris Brown | | Carol Taylor | | Adam Smith | +--------------+ 3 rows in set (0.00 sec)