删除特定字段/行并在 MySQL 中显示其他记录?


为此,在 MySQL 中使用 CASE WHEN 语句。让我们创建一个表 —

mysql> create table demo47
−> (
−> first_name varchar(20),
−> last_name varchar(20)
−> );
Query OK, 0 rows affected (1.57 sec)

使用 insert 命令将一些记录插入表中 —

mysql> insert into demo47 values('John','Smith');
Query OK, 1 row affected (0.18 sec)

mysql> insert into demo47 values('David','Miller');
Query OK, 1 row affected (0.11 sec)

mysql> insert into demo47 values('John','Doe');
Query OK, 1 row affected (0.12 sec)

mysql> insert into demo47 values('Chris','Brown');
Query OK, 1 row affected (0.12 sec)

使用 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)

更新于: 19-11-2020

126 次浏览

开启你的 事业

完成课程获得认证

开始
广告
© . All rights reserved.