SELECT 1 IS NOT NULL, #-- 1
0 IS NOT NULL, #-- 1
NULL IS NOT NULL; #-- 0;
上面查询语句,执行后得到以下结果 -
mysql> SELECT 1 IS NOT NULL, #-- 1
0 IS NOT NULL, #-- 1
NULL IS NOT NULL; #-- 0
+---------------+---------------+------------------+
| 1 IS NOT NULL | 0 IS NOT NULL | NULL IS NOT NULL |
+---------------+---------------+------------------+
| 1 | 1 | 0 |
+---------------+---------------+------------------+
1 row in set
mysql> desc customers;
+------------------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------------+---------------+------+-----+---------+-------+
| customerNumber | int(11) | NO | PRI | NULL | |
| customerName | varchar(50) | NO | | NULL | |
| contactLastName | varchar(50) | NO | | NULL | |
| contactFirstName | varchar(50) | NO | | NULL | |
| phone | varchar(50) | NO | | NULL | |
| addressLine1 | varchar(50) | NO | | NULL | |
| addressLine2 | varchar(50) | YES | | NULL | |
| city | varchar(50) | NO | | NULL | |
| state | varchar(50) | YES | | NULL | |
| postalCode | varchar(15) | YES | | NULL | |
| country | varchar(50) | NO | | NULL | |
| salesRepEmployeeNumber | int(11) | YES | MUL | NULL | |
| creditLimit | decimal(10,2) | YES | | NULL | |
+------------------------+---------------+------+-----+---------+-------+
13 rows in set
要查询没有销售代表的客户,请使用IS NULL运算符,如下所示:
SELECT
customerName,
country,
salesrepemployeenumber
FROM
customers
WHERE
salesrepemployeenumber IS NULL
ORDER BY customerName;
执行上面查询,得到以下结果 -
mysql> SELECT
customerName,
country,
salesrepemployeenumber
FROM
customers
WHERE
salesrepemployeenumber IS NULL
ORDER BY customerName;
+--------------------------------+--------------+------------------------+
| customerName | country | salesrepemployeenumber |
+--------------------------------+--------------+------------------------+
| ANG Resellers | Spain | NULL |
| Anton Designs, Ltd. | Spain | NULL |
| Asian Shopping Network, Co | Singapore | NULL |
| Asian Treasures, Inc. | Ireland | NULL |
| BG&E Collectables | Switzerland | NULL |
| Cramer Spezialitten, Ltd | Germany | NULL |
| Der Hund Imports | Germany | NULL |
| Schuyler Imports | Netherlands | NULL |
| Stuttgart Collectable Exchange | Germany | NULL |
| Warburg Exchange | Germany | NULL |
... ...
+--------------------------------+--------------+------------------------+
22 rows in set
要查询有销售代表的客户,请使用IS NOT NULL运算符,如下查询语句 -
SELECT
customerName,
country,
salesrepemployeenumber
FROM
customers
WHERE
salesrepemployeenumber IS NOT NULL
ORDER BY customerName;
执行上面查询,得到以下结果 -
mysql> SELECT
customerName,
country,
salesrepemployeenumber
FROM
customers
WHERE
salesrepemployeenumber IS NOT NULL
ORDER BY customerName;
+------------------------------------+-------------+------------------------+
| customerName | country | salesrepemployeenumber |
+------------------------------------+-------------+------------------------+
| Alpha Cognac | France | 1370 |
| American Souvenirs Inc | USA | 1286 |
| Amica Models & Co. | Italy | 1401 |
| Anna's Decorations, Ltd | Australia | 1611 |
| Atelier graphique | France | 1370 |
| Australian Collectables, Ltd | Australia | 1611 |
| Australian Collectors, Co. | Australia | 1611 |
| Australian Gift Network, Co | Australia | 1611 |
| Auto Associs & Cie. | France | 1370 |
| Auto Canal+ Petit | France | 1337 |
| Auto-Moto Classics Inc. | USA | 1216 |
| AV Stores, Co. | UK | 1501 |
| Baane Mini Imports | Norway | 1504 |
| Bavarian Collectables Imports, Co. | Germany | 1504 |
... ...
+------------------------------------+-------------+------------------------+
100 rows in set
CREATE TABLE IF NOT EXISTS projects (
id INT AUTO_INCREMENT,
title VARCHAR(255),
begin_date DATE NOT NULL,
complete_date DATE NOT NULL,
PRIMARY KEY(id)
);
INSERT INTO projects(title,begin_date, complete_date)
VALUES('New CRM','2020-01-01','0000-00-00'),
('ERP Future','2020-01-01','0000-00-00'),
('VR','2020-01-01','2030-01-01');
SELECT
*
FROM
projects
WHERE
complete_date IS NULL;