检索行时 MYSQL 中的 AND、OR 运算符有什么区别?

What is Difference Between AND,OR operator in MYSQL ,While Retrieving the Rows?

这些查询和它们的执行方式有区别吗?

SELECT   customername, country, creditLimit
FROM   customers
WHERE (country = 'USA' OR country = 'France') AND creditlimit > 100000;
SELECT    customername, country, creditLimit
FROM    customers
WHERE country = 'USA' OR country = 'France' AND creditlimit > 10000;

这与相交真值表有关。

在第一个查询中,or 在括号内,因此它将首先执行,如果国家是美国或法国,则 return true。然后 And 将执行并将 OR 的结果与 creditlimit > 100000 的结果进行比较。

在第二个查询中,运算符优先级接管,因此无论是 Or 还是 And 都先执行。根据这个网站 https://www.mysqltutorial.org/mysql-or/

Operator precedence When you use more than one logical operator in an expression, MySQL always evaluates the OR operators after the AND operators. This is called operator precedence which determines the order of evaluation of the operators. MySQL evaluates the operator with higher precedence first.

这意味着在此查询中数据库将检查国家是否为法国以及信用额度 > 10000 然后运行结果与国家是否为美国之间的 OR 语句。

ETA:用简单的英语表示,第二个查询将 return 国家为美国的所有结果,无论信用额度如何,它还将 return 国家为法国和信用额度 > 10000.

您要运行 查询的最大问题是操作顺序。请查看以下有关运算符优先级的文章 (Link)

由于 AND 具有更高的优先级,您的第二个查询解释如下:

SELECT customername, country, creditLimit FROM customers WHERE country = 'USA' OR (country = 'France' AND creditlimit > 10000);

所以第二个查询只查找国家是美国或国家是法国并且信用额度 > 10,000。