为此,请在MySQL中使用CASE WHEN语句。让我们创建一个表-
mysql> create table demo47 −> ( −> first_name varchar(20), −> last_name varchar(20) −> );
借助insert命令将一些记录插入表中-
mysql> insert into demo47 values('John','Smith'); mysql> insert into demo47 values('David','Miller'); mysql> insert into demo47 values('John','Doe'); mysql> insert into demo47 values('Chris','Brown');
使用select语句显示表中的记录-
mysql> select *from demo47;
这将产生以下输出-
+------------+-----------+ | first_name | last_name | +------------+-----------+ | John | Smith | | David | Miller | | John | Doe | | Chris | Brown | +------------+-----------+ 4 rows in set (0.00 sec)
以下是删除特定字段/行并在MySQL中显示其他记录的查询。
mysql> select case when first_name= 'John' then last_name else first_name end Result −> from demo47 −> where 'John' in (first_name,last_name);
这将产生以下输出-
+--------+ | Result | +--------+ | Smith | | Doe | +--------+ 2 rows in set (0.00 sec)