如何通过在MySQL中连接来选择带条件的行?
为此,您可以使用CONCAT_WS()。 让我们创建一个表 –
mysql> create table demo38
−> (
−> user_id int,
−> user_first_name varchar(20),
−> user_last_name varchar(20),
−> user_date_of_birth date
−> );
QUERY OK,0 ROWS affected (1.70 sec)
通过插入命令将一些记录插入表中 –
mysql> insert into demo38 values(10,'John','Smith','1990−10−01');
QUERY OK,1 ROW affected (0.14 sec)
mysql> insert into demo38 values(11,'David','Miller','1994−01−21');
QUERY OK,1 ROW affected (0.13 sec)
mysql> insert into demo38 values(11,'John','Doe','1992−02−01');
QUERY OK,1 ROW affected (0.13 sec)
mysql> insert into demo38 values(12,'Adam','Smith','1996−11−11');
QUERY OK,1 ROW affected (0.11 sec)
mysql> insert into demo38 values(13,'Chris','Brown','1997−03−10');
QUERY OK,1 ROW affected (0.13 sec)
使用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行,需要(0.00秒)
以下是用于选择带有条件行的查询 –
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行,需要(0.00秒)
阅读更多:MySQL 教程
极客教程