2.4 AND运算符
WHERE boolean_expression_1 AND boolean_expression_2 SQL 以下说明
当求值具有 SELECT 1 = 0 AND 1 / 0 ; Shell 执行上面查询时,得到以下结果 - mysql> SELECT 1 = 0 AND 1 / 0 ;
-----------------
| 1 = 0 AND 1 / 0 |
-----------------
| 0 |
-----------------
1 row in set Shell
MySQL只计算表达式 MySQL AND运算符示例下面使用示例数据库中的 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 以下声明选择国家是 SELECT customername, country, state FROM customers WHERE country = 'USA' AND state = 'CA'; 执行上面查询得到以下结果 - mysql> SELECT customername, country, state FROM customers WHERE country = 'USA' AND state = 'CA';
------------------------------ --------- -------
| customername | country | state |
------------------------------ --------- -------
| Mini Gifts Distributors Ltd. | USA | CA |
| Mini Wheels Co. | USA | CA |
| Technics Stores Inc. | USA | CA |
| Toys4GrownUps.com | USA | CA |
| Boards & Toys Co. | USA | CA |
| Collectable Mini Designs Co. | USA | CA |
| Corporate Gift Ideas Co. | USA | CA |
| Men 'R' US Retailers, Ltd. | USA | CA |
| The Sharp Gifts Warehouse | USA | CA |
| West Coast Collectables Co. | USA | CA |
| Signal Collectibles Ltd. | USA | CA |
------------------------------ --------- -------
11 rows in set Shell 使用 SELECT customername,
country,
state,
creditlimit
FROM customers
WHERE country = 'USA'
AND state = 'CA'
AND creditlimit > 100000; Shell 执行上面查询语句,得到以下结果 - mysql> SELECT customername, country, state, creditlimit FROM customers
WHERE country = 'USA' AND state = 'CA' AND creditlimit > 100000;
------------------------------ --------- ------- -------------
| customername | country | state | creditlimit |
------------------------------ --------- ------- -------------
| Mini Gifts Distributors Ltd. | USA | CA | 210500 |
| Collectable Mini Designs Co. | USA | CA | 105000 |
| Corporate Gift Ideas Co. | USA | CA | 105000 |
------------------------------ --------- ------- -------------
3 rows in set 2.5 OR运算符 MySQL 下面说明了 boolean_expression_1 OR boolean_expression_2 Shell
下表显示了
MySQL OR短路求值MySQL使用 请参见以下示例。 SELECT 1 = 1 OR 1 / 0; SQL 执行上面代码,得到以下结果 - mysql> SELECT 1 = 1 OR 1 / 0;
----------------
| 1 = 1 OR 1 / 0 |
----------------
| 1 |
----------------
1 row in set Shell 因为表达式 运算符优先级当您在语句中使用多个逻辑运算符时,MySQL会在 运算符优先级决定运算符的求值顺序。 MySQL首先对优先级较高的运算符进行求值。 请参见以下示例。 SELECT true OR false AND false; SQL 执行上面查询,得到如下结果 - mysql> SELECT true OR false AND false;
-------------------------
| true OR false AND false |
-------------------------
| 1 |
-------------------------
1 row in set Shell 上面得出的结果,运算的过程是怎么样呢?
要更改评估/求值的顺序,请使用括号,例如: SELECT (true OR false) AND false; SQL 执行上面查询,得到如下结果 - mysql> SELECT (true OR false) AND false;
---------------------------
| (true OR false) AND false |
---------------------------
| 0 |
---------------------------
1 row in set Shell 上面得出的结果,运算的过程是怎么样呢?
MySQL OR运算符示例下面,我们将使用示例数据库(yiibaidb)中的 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 Shell 例如,要获得美国( SELECT
customername, country SQL 执行上面代码,得到如下结果 - mysql> SELECT customername, country FROM customers WHERE country = 'USA' OR country = 'France';
------------------------------ ---------
| customername | country |
------------------------------ ---------
| Atelier graphique | France |
| Signal Gift Stores | USA |
| La Rochelle Gifts | France |
| Mini Gifts Distributors Ltd. | USA |
| Mini Wheels Co. | USA |
| Land of Toys Inc. | USA |
| Saveley & Henriot, Co. | France |
| Muscle Machine Inc | USA |
| Diecast Classics Inc. | USA |
| Technics Stores Inc. | USA |
... ...
------------------------------ ---------
48 rows in set Shell 以下声明返回位于美国( SELECT
customername, country, creditLimit SQL 执行上面代码,得到如下结果 - mysql> SELECT customername, country, creditLimit FROM customers WHERE (country = 'USA' OR country = 'France') AND creditlimit > 100000;
------------------------------ --------- -------------
| customername | country | creditLimit |
------------------------------ --------- -------------
| La Rochelle Gifts | France | 118200 |
| Mini Gifts Distributors Ltd. | USA | 210500 |
| Land of Toys Inc. | USA | 114900 |
| Saveley & Henriot, Co. | France | 123900 |
| Muscle Machine Inc | USA | 138500 |
| Diecast Classics Inc. | USA | 100600 |
| Collectable Mini Designs Co. | USA | 105000 |
| Marta's Replicas Co. | USA | 123700 |
| Mini Classics | USA | 102700 |
| Corporate Gift Ideas Co. | USA | 105000 |
| Online Diecast Creations Co. | USA | 114200 |
------------------------------ --------- -------------
11 rows in set Shell 请注意,如果不使用括号,查询将返回位于美国的客户或者位于法国并且信用额度大于 SELECT
customername, country, creditLimit SQL 执行上面代码,得到如下结果(共 mysql> SELECT customername, country, creditLimit FROM customers WHERE country = 'USA' OR country = 'France' AND creditlimit > 100000; ------------------------------ --------- ------------- | customername | country | creditLimit | ------------------------------ --------- ------------- | Signal Gift Stores | USA | 71800 || La Rochelle Gifts | France | 118200 || Mini Gifts Distributors Ltd. | USA | 210500 || Mini Wheels Co. | USA | 64600 || Land of Toys Inc. | USA | 114900 || Saveley & Henriot, Co. | France | 123900 || Muscle Machine Inc | USA | 138500 || Diecast Classics Inc. | USA | 100600 || Technics Stores Inc. | USA | 84600 || American Souvenirs Inc | USA | 0 || Cambridge Collectables Co. | USA | 43400 || Gift Depot Inc. | USA | 84300 || Vitachrome Inc. | USA | 76400 || Auto-Moto Classics Inc. | USA | 23000 || Online Mini Collectables | USA | 68700 || Toys4GrownUps.com | USA | 90700 || Boards & Toys Co. | USA | 11000 || Collectable Mini Designs Co. | USA | 105000 || Marta's Replicas Co. | USA | 123700 |
| Mini Classics | USA | 102700 |
| Mini Creations Ltd. | USA | 94500 |
| Corporate Gift Ideas Co. | USA | 105000 |
| Tekni Collectables Inc. | USA | 43000 |
| Classic Gift Ideas, Inc | USA | 81100 |
| Men 'R' US Retailers, Ltd. | USA | 57700 || Gifts4AllAges.com | USA | 41900 || Online Diecast Creations Co. | USA | 114200 || Collectables For Less Inc. | USA | 70700 || Classic Legends Inc. | USA | 67500 || Gift Ideas Corp. | USA | 49700 || The Sharp Gifts Warehouse | USA | 77600 || Super Scale Inc. | USA | 95400 || Microscale Inc. | USA | 39800 || FunGiftIdeas.com | USA | 85800 || West Coast Collectables Co. | USA | 55400 || Motor Mint Distributors Inc. | USA | 72600 || Signal Collectibles Ltd. | USA | 60300 || Diecast Collectables | USA | 85100 | ------------------------------ --------- ------------- 38 rows in set 2.6 IN运算符 IN运算符允许您确定指定的值是否与列表中的值或子查询中的任何值匹配。 下面说明了 SELECT
column1,column2,... SQL 下面我们更详细的来看看上面的查询:
如果 当列表中的值都是常量时:
可以将 MySQL IN示例下面练习一些使用 mysql> desc offices;
-------------- ------------- ------ ----- --------- -------
| Field | Type | Null | Key | Default | Extra |
-------------- ------------- ------ ----- --------- -------
| officeCode | varchar(10) | NO | PRI | NULL | |
| city | varchar(50) | NO | | NULL | |
| phone | varchar(50) | NO | | NULL | |
| addressLine1 | varchar(50) | NO | | NULL | |
| addressLine2 | varchar(50) | YES | | NULL | |
| state | varchar(50) | YES | | NULL | |
| country | varchar(50) | NO | | NULL | |
| postalCode | varchar(15) | NO | | NULL | |
| territory | varchar(10) | NO | | NULL | |
-------------- ------------- ------ ----- --------- -------
9 rows in set Shell 如果您想查找位于美国和法国的办事处,可以使用 SELECT
officeCode, city, phone, country SQL 执行上面查询语句,得到以下结果 - mysql> SELECT officeCode, city, phone, country FROM offices WHERE country IN ('USA' , 'France');
------------ --------------- ----------------- ---------
| officeCode | city | phone | country |
------------ --------------- ----------------- ---------
| 1 | San Francisco | 1 650 219 4782 | USA |
| 2 | Boston | 1 215 837 0825 | USA |
| 3 | NYC | 1 212 555 3000 | USA |
| 4 | Paris | 33 14 723 4404 | France |
------------ --------------- ----------------- ---------
4 rows in set Shell 也可以使用 SELECT
officeCode, city, phone SQL 执行上面查询语句,得到以下结果 - mysql> SELECT officeCode, city, phone FROM offices WHERE country = 'USA' OR country = 'France';
------------ --------------- -----------------
| officeCode | city | phone |
------------ --------------- -----------------
| 1 | San Francisco | 1 650 219 4782 |
| 2 | Boston | 1 215 837 0825 |
| 3 | NYC | 1 212 555 3000 |
| 4 | Paris | 33 14 723 4404 |
------------ --------------- -----------------
4 rows in set Shell 如果列表中有很多值,使用多个 要获得不在美国和法国的办事处,请在 SELECT
officeCode, city, phoneFROM
officesWHERE
country NOT IN ('USA' , 'France'); SQL 执行上面查询语句,得到以下结果 - mysql> SELECT officeCode, city, phone FROM offices WHERE country NOT IN( 'USA', 'France');
------------ --------- ------------------
| officeCode | city | phone |
------------ --------- ------------------
| 5 | Beijing | 86 33 224 5000 |
| 6 | Sydney | 61 2 9264 2451 |
| 7 | London | 44 20 7877 2041 |
------------ --------- ------------------
3 rows in set Shell MySQL IN与子查询
我们来看看两张表: 例如,如果要查找总金额大于 SELECT
orderNumber, customerNumber, status, shippedDate 执行上面语句,得到以下结果 - mysql> SELECT
orderNumber, customerNumber, status, shippedDate
FROM
orders
WHERE
orderNumber IN (SELECT
orderNumber
FROM
orderDetails
GROUP BY orderNumber
HAVING SUM(quantityOrdered * priceEach) > 60000);
------------- ---------------- --------- -------------
| orderNumber | customerNumber | status | shippedDate |
------------- ---------------- --------- -------------
| 10165 | 148 | Shipped | 2013-12-26 |
| 10287 | 298 | Shipped | 2014-09-01 |
| 10310 | 259 | Shipped | 2014-10-18 |
------------- ---------------- --------- -------------
3 rows in set Shell 上面的整个查询可以分为 首先,子查询使用 SELECT
orderNumber SQL 执行上面语句,得到以下结果 - mysql> SELECT
orderNumber
FROM
orderDetails
GROUP BY orderNumber
HAVING SUM(quantityOrdered * priceEach) > 60000;
-------------
| orderNumber |
-------------
| 10165 |
| 10287 |
| 10310 |
-------------
3 rows in set Shell 其次,主查询从 SELECT
orderNumber, customerNumber, status, shippedDate SQL 执行上面语句,得到以下结果 - mysql> SELECT
orderNumber, customerNumber, status, shippedDate
FROM
orders
WHERE
orderNumber IN (10165,10287,10310);
------------- ---------------- --------- -------------
| orderNumber | customerNumber | status | shippedDate |
------------- ---------------- --------- -------------
| 10165 | 148 | Shipped | 2013-12-26 |
| 10287 | 298 | Shipped | 2014-09-01 |
| 10310 | 259 | Shipped | 2014-10-18 |
------------- ---------------- --------- -------------
3 rows in set 2.7 BETWEEN运算符
下面说明了 expr [NOT] BETWEEN begin_expr AND end_expr; SQL
所有三个表达式: 如果 如果 如果任何表达式为 MySQL BETWEEN示例下面我们来练习一些使用 MySQL BETWEEN与数字示例 请参见示例数据库(yiibaidb)中的以下产品( mysql> desc products;
-------------------- --------------- ------ ----- --------- -------
| Field | Type | Null | Key | Default | Extra |
-------------------- --------------- ------ ----- --------- -------
| productCode | varchar(15) | NO | PRI | NULL | |
| productName | varchar(70) | NO | | NULL | |
| productLine | varchar(50) | NO | MUL | NULL | |
| productScale | varchar(10) | NO | | NULL | |
| productVendor | varchar(50) | NO | | NULL | |
| productDescription | text | NO | | NULL | |
| quantityInStock | smallint(6) | NO | | NULL | |
| buyPrice | decimal(10,2) | NO | | NULL | |
| MSRP | decimal(10,2) | NO | | NULL | |
-------------------- --------------- ------ ----- --------- -------
9 rows in set Shell 假设您想要查找价格在 mysql> SELECT
productCode, productName, buyPrice
FROM
products
WHERE
buyPrice BETWEEN 90 AND 100;
------------- -------------------------------------- ----------
| productCode | productName | buyPrice |
------------- -------------------------------------- ----------
| S10_1949 | 1952 Alpine Renault 1300 | 98.58 |
| S10_4698 | 2003 Harley-Davidson Eagle Drag Bike | 91.02 |
| S12_1099 | 1968 Ford Mustang | 95.34 |
| S12_1108 | 2001 Ferrari Enzo | 95.59 |
| S18_1984 | 1995 Honda Civic | 93.89 |
| S18_4027 | 1970 Triumph Spitfire | 91.92 |
| S24_3856 | 1956 Porsche 356A Coupe | 98.3 |
------------- -------------------------------------- ----------
7 rows in set Shell 也可以通过使用大于或等于( mysql> SELECT
productCode, productName, buyPrice
FROM
products
WHERE
buyPrice >= 90 AND buyPrice <= 100;
------------- -------------------------------------- ----------
| productCode | productName | buyPrice |
------------- -------------------------------------- ----------
| S10_1949 | 1952 Alpine Renault 1300 | 98.58 |
| S10_4698 | 2003 Harley-Davidson Eagle Drag Bike | 91.02 |
| S12_1099 | 1968 Ford Mustang | 95.34 |
| S12_1108 | 2001 Ferrari Enzo | 95.59 |
| S18_1984 | 1995 Honda Civic | 93.89 |
| S18_4027 | 1970 Triumph Spitfire | 91.92 |
| S24_3856 | 1956 Porsche 356A Coupe | 98.3 |
------------- -------------------------------------- ----------
7 rows in set Shell 要查找购买价格不在 您也可以使用少于( SELECT
productCode, productName, buyPrice SQL 执行上面查询语句,得到以下结果 - mysql> SELECT
productCode, productName, buyPrice
FROM
products
WHERE
buyPrice < 20 OR buyPrice > 100;
------------- ------------------------------------- ----------
| productCode | productName | buyPrice |
------------- ------------------------------------- ----------
| S10_4962 | 1962 LanciaA Delta 16V | 103.42 |
| S18_2238 | 1998 Chrysler Plymouth Prowler | 101.51 |
| S24_2840 | 1958 Chevy Corvette Limited Edition | 15.91 |
| S24_2972 | 1982 Lamborghini Diablo | 16.24 |
------------- ------------------------------------- ----------
4 rows in set Shell MySQL BETWEEN与日期类型数据示例当使用 例如,要查询获取所需日期( SELECT orderNumber,
requiredDate,
status SQL 执行上面查询语句,得到以下结果 - mysql> SELECT orderNumber,
requiredDate,
status
FROM orders
WHERE requireddate
BETWEEN CAST('2013-01-01' AS DATE)
AND CAST('2013-01-31' AS DATE);
------------- -------------- ---------
| orderNumber | requiredDate | status |
------------- -------------- ---------
| 10100 | 2013-01-13 | Shipped |
| 10101 | 2013-01-18 | Shipped |
| 10102 | 2013-01-18 | Shipped |
------------- -------------- ---------
3 rows in set Shell 因为 2.8 IS NULL运算符 要测试值是否为 value IS NULL SQL 如果值为 请注意,MySQL没有内置的BOOLEAN类型。 它使用TINYINT(1)来表示 因为 SELECT 1 IS NULL, # -- 0
0 IS NULL, # -- 0
NULL IS NULL; # -- 1; SQL 要检查值是否不为 value IS NOT NULL SQL 如果该值不为 SELECT 1 IS NOT NULL, #-- 1
0 IS NOT NULL, #-- 1
NULL IS NOT NULL; #-- 0; SQL 上面查询语句,执行后得到以下结果 - 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 Shell 2. MySQL IS NULL示例我们将使用示例数据库(yiibaidb)中的 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 SQL 要查询没有销售代表的客户,请使用 SELECT
customerName,
country,
salesrepemployeenumberFROM
customersWHERE
salesrepemployeenumber IS NULLORDER BY customerName; SQL 执行上面查询,得到以下结果 - 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 Shell 要查询有销售代表的客户,请使用 SELECT
customerName,
country,
salesrepemployeenumberFROM
customersWHERE
salesrepemployeenumber IS NOT NULLORDER BY customerName; SQL 执行上面查询,得到以下结果 - 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 Shell 3. MySQL IS NULL的专用功能为了兼容ODBC程序,MySQL支持 (1). 如果具有 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)); SQL 在这个例子中,创建了一个 (2). 如果变量 首先,将变量 SET @@sql_auto_is_null = 1; SQL 第二步,在 INSERT INTO projects(title,begin_date, complete_date)VALUES('MRP III','2010-01-01','2020-12-31'); SQL 第三步,使用 SELECT
id SQL 4. MySQL IS NULL优化MySQL对于 例如,MySQL在使用 SELECT
customerNumber,
salesRepEmployeeNumberFROM
customersWHERE
salesRepEmployeeNumber IS NULL; SQL 查看 EXPLAIN SELECT
customerNumber,
salesRepEmployeeNumberFROM
customersWHERE
salesRepEmployeeNumber IS NULL; SQL 执行上面查询语句,输出以下结果 - mysql> EXPLAIN SELECT
customerNumber,
salesRepEmployeeNumber
FROM
customers
WHERE
salesRepEmployeeNumber IS NULL;
---- ------------- ----------- ------------ ------ ------------------------ ------------------------ --------- ------- ------ ---------- --------------------------
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
---- ------------- ----------- ------------ ------ ------------------------ ------------------------ --------- ------- ------ ---------- --------------------------
| 1 | SIMPLE | customers | NULL | ref | salesRepEmployeeNumber | salesRepEmployeeNumber | 5 | const | 22 | 100 | Using where; Using index |
---- ------------- ----------- ------------ ------ ------------------------ ------------------------ --------- ------- ------ ---------- --------------------------
1 row in set Shell MySQL也可以优化组合 EXPLAIN SELECT
customerNumber,
salesRepEmployeeNumber SQL 执行上面查询语句,得到以下结果 - mysql> EXPLAIN SELECT
customerNumber,
salesRepEmployeeNumber
FROM
customers
WHERE
salesRepEmployeeNumber = 1370 OR
salesRepEmployeeNumber IS NULL;
---- ------------- ----------- ------------ ------------- ------------------------ ------------------------ --------- ------- ------ ---------- --------------------------
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
---- ------------- ----------- ------------ ------------- ------------------------ ------------------------ --------- ------- ------ ---------- --------------------------
| 1 | SIMPLE | customers | NULL | ref_or_null | salesRepEmployeeNumber | salesRepEmployeeNumber | 5 | const | 29 | 100 | Using where; Using index |
---- ------------- ----------- ------------ ------------- ------------------------ ------------------------ --------- ------- ------ ---------- --------------------------
1 row in set Shell 在这个例子中,当应用优化时, 如果您有一个列的组合键,MySQL可以对任何关键部分执行优化。假设在表 SELECT
* SQL |
|