为此,您可以使用CONCAT_WS()。让我们创建一个表-
mysql> create table demo38 −> ( −> user_id int, −> user_first_name varchar(20), −> user_last_name varchar(20), −> user_date_of_birth date −> );
借助insert命令将一些记录插入表中-
mysql> insert into demo38 values(10,'John','Smith','1990−10−01'); mysql> insert into demo38 values(11,'David','Miller','1994−01−21'); mysql> insert into demo38 values(11,'John','Doe','1992−02−01'); mysql> insert into demo38 values(12,'Adam','Smith','1996−11−11'); mysql> insert into demo38 values(13,'Chris','Brown','1997−03−10');
使用select语句显示表中的记录-
mysql> select *from demo38;
这将产生以下输出-
+---------+-----------------+----------------+--------------------+ | user_id | user_first_name | user_last_name | user_date_of_birth | +---------+-----------------+----------------+--------------------+ | 10 | John | Smith | 1990−10−01 | | 11 | David | Miller | 1994−01−21 | | 11 | John | Doe | 1992−02−01 | | 12 | Adam | Smith | 1996−11−11 | | 13 | Chris | Brown | 1997−03−10 | +---------+-----------------+----------------+--------------------+ 5 rows in set (0.00 sec)
以下是选择条件的行的查询-
mysql> select concat_ws('/',user_first_name, user_last_name,'the date of birth year is=', date_format(user_date_of_birth,'%Y')) as Output −> from demo38 −> where user_id in(11,13);
这将产生以下输出-
+----------------------------------------------+ | Output | +----------------------------------------------+ | David/Miller/the date of birth year is=/1994 | | John/Doe/the date of birth year is=/1992 | | Chris/Brown/the date of birth year is=/1997 | +----------------------------------------------+ 3 rows in set (0.00 sec)