zoukankan      html  css  js  c++  java
  • A2-02-05.DML-MySQL OR Operator

    转载自:http://www.mysqltutorial.org/mysql-or/

    MySQL OR Operator

     

    Summary: in this tutorial, you will learn how to use the MySQL OR operator to combine Boolean expressions for filtering data.

    Introduction to the MySQL OR operator

    The MySQL OR operator combines two Boolean expressions and returns true when either condition is true.

    The following illustrates the syntax of the OR operator.

    Both boolean_expression_1 and boolean_expression_2 are Boolean expressions that return true, false, or NULL.

    The following table shows the result of the OR operator.

      TRUE FALSE NULL
    TRUE TRUE TRUE TRUE
    FALSE TRUE FALSE NULL
    NULL TRUE NULL NULL

    MySQL OR short-circuit evaluation

    MySQL uses short-circuit evaluation for the OR operator. In other words, MySQL stops evaluating the remaining parts of the statement when it can determine the result.

    See the following example.

    Try It Out

    Because the expression 1 = 1 always returns true, MySQL does not evaluate the 1 / 0. If it did, it would issue an error because of the division by zero error.

    Operator precedence

    When you use more than one logical operator in an expression, MySQL evaluates the OR operators after the AND operators. This is called operator precedence.

    The operator precedence determines the order of evaluation of the operators. MySQL evaluates the operator with the higher precedence first.

    See the following example.

    Try It Out

    How it works

    1. First, MySQL evaluates the AND operator, therefore the expression false AND false returns false.
    2. Second, MySQL evaluates the OR operator hence the expression true OR false returns true.

    To change the order of evaluation, you use the parentheses, for example:

    Try It Out

    How it works

    1. First, MySQL evaluates the expression in the parenthesis (true OR false) returns true
    2. Second, MySQL evaluates the remaining part of the statement, true AND false returns false.

    MySQL OR operator examples

    We will use the  customers table in the sample database for the demonstration.

    customers_table

    For example, to get the customers who locate in the USA or France, you use the OR operator in the WHERE clause as follows:

    Try It Out

    MySQL OR example

    As you can see the result, the query returns customers who locate in either USA or France.

    The following statement returns the customers who locate in the USA or France and have credit limit greater than 10000.

    Try It Out

    MySQL OR AND example

    Notice that if you do not use the parentheses, the query will return the customers who locate in the USA or the customers who locate in France with the credit limit greater than 10000.

    Try It Out

    MySQL operator precedence example

    In this tutorial, you have learned how to use the MySQL OR operator to combine Boolean expressions for filtering data.

  • 相关阅读:
    回溯算法总结
    第四章总结
    第四章编程总结
    动态规划总结:
    第三章实践心得
    分治算法体会
    第二章上机实践总结
    代码规范与《数学之美》读后感
    第二次c++作业
    第一次博客作业
  • 原文地址:https://www.cnblogs.com/zhuntidaoren/p/9511553.html
Copyright © 2011-2022 走看看