刪除特定欄位/行並在 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 次瀏覽

開啟你的 事業

完成課程獲得認證

開始
廣告