OR

运算符说明

OR,|| 逻辑运算符用作逻辑或运算。当两个操作数都非 null 时,如果操作数同时也非零,则返回结果为 true,否则为 false;对于 NULL 操作数,如果另一个操作数非零,则返回结果为 true,否则为 NULL;如果两个操作数都为 NULL,则返回结果为 NULL

语法结构

  1. > SELECT column_1 OR column_2 FROM table_name;

示例

  1. mysql> select 1 or 1;
  2. +--------+
  3. | 1 or 1 |
  4. +--------+
  5. | true |
  6. +--------+
  7. 1 row in set (0.01 sec)
  8. mysql> select 1 or 0;
  9. +--------+
  10. | 1 or 0 |
  11. +--------+
  12. | true |
  13. +--------+
  14. 1 row in set (0.00 sec)
  15. mysql> select 0 or 0;
  16. +--------+
  17. | 0 or 0 |
  18. +--------+
  19. | false |
  20. +--------+
  21. 1 row in set (0.01 sec)
  22. mysql> select 0 or null;
  23. +-----------+
  24. | 0 or null |
  25. +-----------+
  26. | NULL |
  27. +-----------+
  28. 1 row in set (0.00 sec)
  29. mysql> select 1 or null;
  30. +-----------+
  31. | 1 or null |
  32. +-----------+
  33. | true |
  34. +-----------+
  35. 1 row in set (0.00 sec)
  1. create table t1 (a boolean,b bool);
  2. insert into t1 values (0,1),(true,false),(true,1),(0,false),(NULL,NULL);
  3. mysql> select a or b from t1;
  4. +--------+
  5. | a or b |
  6. +--------+
  7. | true |
  8. | true |
  9. | true |
  10. | false |
  11. | NULL |
  12. +--------+
  13. 5 rows in set (0.00 sec)