MySQL 只计算表达式1 = 0 AND 1/0的第一部分1 = 0,因为表达式1 = 0返回false,所以 MySQL 得出结论:整个表达式的结果是false。 MySQL 不对表达式的剩余部分求值,即不对1/0进行求值; 如果对1/0进行求值,它将发出一个错误消息,因为除以零错误。
MySQL AND 运算符示例
下面使用示例数据库中的customers表进行演示。customers表的结构如下所示 -
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 | |+------------------------+---------------+------+-----+---------+-------+13rowsinset
以下声明选择国家是USA和CA的客户。我们在WHERE子句中使用AND运算符。
SELECT customername, country, stateFROM customers WHERE country ='USA'ANDstate='CA';
执行上面查询得到以下结果 -
mysql>SELECT customername, country, stateFROM customers WHERE country ='USA'ANDstate='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 |+------------------------------+---------+-------+11rowsinset
SELECT customername, country,state, creditlimitFROM customersWHERE country ='USA'ANDstate='CA'AND creditlimit >100000;
执行上面查询语句,得到以下结果 -
mysql>SELECT customername, country, state, creditlimit FROM customersWHERE country ='USA'ANDstate='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 |+------------------------------+---------+-------+-------------+3rowsinset
在本教程中,我们向您展示了如何使用 MySQL AND运算符组合两个或多个表达式以形成WHERE子句的复合条件语句。