MySQL 中的 WHERE IN null 如何使用?


以下是語法 −

select
yourColumnName1,
yourColumnName2,
yourColumnName3,
.
.
.
N
from yourTableName
where yourValue in(yourColumnName1,yourColumnName2) or yourColumnName1 is NULL;

讓我們建立一個表 −

mysql> create table demo60
−> (
−> id int not null auto_increment primary key,
−> first_name varchar(20),
−> last_name varchar(20)
−> )
−> ;
Query OK, 0 rows affected (2.11 sec)

使用 insert 命令插入一些記錄到表中 −

mysql> insert into demo60(first_name,last_name) values('John','Smith');
Query OK, 1 row affected (0.09 sec)

mysql> insert into demo60(first_name,last_name) values('John','Doe');
Query OK, 1 row affected (0.51 sec)

mysql> insert into demo60(first_name,last_name) values(null,'Brown');
Query OK, 1 row affected (0.09 sec)

mysql> insert into demo60(first_name,last_name) values('David','Miller');
Query OK, 1 row affected (0.16 sec)

mysql> insert into demo60(first_name,last_name) values('David','Smith');
Query OK, 1 row affected (0.11 sec)

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

使用 select 語句從表中顯示記錄 −

mysql> select *from demo60;

這將產生以下輸出 −

+----+------------+-----------+
| id | first_name | last_name |
+----+------------+-----------+
|  1 | John       | Smith     |
|  2 | John       | Doe       |
|  3 | NULL       | Brown     |
|  4 | David      | Miller    |
|  5 | David      | Smith     |
|  6 | Chris      | Brown     |
+----+------------+-----------+
6 rows in set (0.00 sec)

以下是使用 NULL 選擇 where in 的查詢 −

Mysql> select
−> id,
−> first_name,
−> last_name
−> from demo60
−> where 'John' in(first_name,last_name) or first_name is NULL;

這將產生以下輸出 −

+----+------------+-----------+
| id | first_name | last_name |
+----+------------+-----------+
| 1  | John       | Smith     |
| 2  | John       | Doe       |
| 3  | NULL       | Brown     |
+----+------------+-----------+
3 rows in set (0.00 sec)

更新於: 2020 年 11 月 20 日

445 次瀏覽

開啟你的職業

完成課程,獲得認證

開始
廣告
© . All rights reserved.