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.

  • 相关阅读:
    POJ 2456 Aggressive cows
    POJ 1064 Cable master
    POJ 3723 Conscription
    左偏树
    tarjan模板
    [bzoj5017][Snoi2017]炸弹 tarjan缩点+线段树优化建图+拓扑
    [BZOJ4520][Cqoi2016]K远点对 kd-tree 优先队列
    [bzoj3218]a + b Problem 网络流+主席树优化建图
    #6034. 「雅礼集训 2017 Day2」线段游戏 李超树
    【UOJ UNR #1】火车管理 可持久化线段树
  • 原文地址:https://www.cnblogs.com/zhuntidaoren/p/9511553.html
Copyright © 2011-2022 走看看