1. +,-,*,/(DIV),%(MOD)
  2. select 1 + 2;
  3. select 1 - 2;
  4. select 1 * 2;
  5. select 2 / 3;
  6. mysql> select 2/3;
  7. +--------+
  8. | 2/3 |
  9. +--------+
  10. | 0.6667 |
  11. +--------+
  12. select 10 DIV 4;
  13. mysql> select 10 DIV 4;
  14. +----------+
  15. | 10 DIV 4 |
  16. +----------+
  17. | 2 |
  18. +----------+
  19. select 10 MOD 4;
  20. mysql> select 10 MOD 4;
  21. +----------+
  22. | 10 DIV 4 |
  23. +----------+
  24. | 2 |
  25. +----------+

比较运算符

  1. mysql> select 2=3; // 0
  2. mysql> select NULL = NULL; // NULL
  3. mysql> select 2<>3; // 1
  4. ### 安全等于, 与 = 的区别在于当两个操作码均为 NULL 时,其所得值为 1 而不为 NULL,
  5. ### 而当一个操作码为 NULL 时,其所得值为 0而不为 NULL。
  6. mysql> select 2<=>3; // 0 // 安全等于
  7. mysql> select 2<3; // 1
  8. mysql> select 2<=3; // 1
  9. mysql> select 2>3; // 0
  10. mysql> select 2>=3; // 0
  11. mysql> select 5 between 1 and 10; // 什么到什么之间
  12. +--------------------+
  13. | 5 between 1 and 10 |
  14. +--------------------+
  15. | 1 |
  16. +--------------------+
  17. mysql> select 5 in (1,2,3,4,5); // 在集合中
  18. +------------------+
  19. | 5 in (1,2,3,4,5) |
  20. +------------------+
  21. | 1 |
  22. +------------------+
  23. mysql> select 5 not in (1,2,3,4,5); // 不在集合中
  24. +----------------------+
  25. | 5 not in (1,2,3,4,5) |
  26. +----------------------+
  27. | 0 |
  28. +----------------------+
  29. mysql> select null is NULL;
  30. +--------------+
  31. | null is NULL |
  32. +--------------+
  33. | 1 |
  34. +--------------+
  35. mysql> select 'a' is NULL;
  36. +-------------+
  37. | 'a' is NULL |
  38. +-------------+
  39. | 0 |
  40. +-------------+
  41. mysql> select null IS NOT NULL;
  42. +------------------+
  43. | null IS NOT NULL |
  44. +------------------+
  45. | 0 |
  46. +------------------+
  47. mysql> select 'a' IS NOT NULL;
  48. +-----------------+
  49. | 'a' IS NOT NULL |
  50. +-----------------+
  51. | 1 |
  52. +-----------------+
  53. mysql> select '12345' like '12%';
  54. +--------------------+
  55. | '12345' like '12%' |
  56. +--------------------+
  57. | 1 |
  58. +--------------------+
  59. mysql> select '12345' like '12_';
  60. +--------------------+
  61. | '12345' like '12_' |
  62. +--------------------+
  63. | 0 |
  64. +--------------------+
  65. mysql> select 'beijing' REGEXP 'jing';
  66. +-------------------------+
  67. | 'beijing' REGEXP 'jing' |
  68. +-------------------------+
  69. | 1 |
  70. +-------------------------+
  71. mysql> select 'beijing' REGEXP 'xi';
  72. +-----------------------+
  73. | 'beijing' REGEXP 'xi' |
  74. +-----------------------+
  75. | 0 |
  76. +-----------------------+

逻辑运算符

  1. mysql> select 2 and 0;
  2. +---------+
  3. | 2 and 0 |
  4. +---------+
  5. | 0 |
  6. +---------+
  7. mysql> select 2 and 1;
  8. +---------+
  9. | 2 and 1 |
  10. +---------+
  11. | 1 |
  12. +---------+
  13. mysql> select 2 or 0;
  14. +--------+
  15. | 2 or 0 |
  16. +--------+
  17. | 1 |
  18. +--------+
  19. mysql> select 2 or 1;
  20. +--------+
  21. | 2 or 1 |
  22. +--------+
  23. | 1 |
  24. +--------+
  25. mysql> select 0 or 0;
  26. +--------+
  27. | 0 or 0 |
  28. +--------+
  29. | 0 |
  30. +--------+
  31. mysql> select 1 || 0;
  32. +--------+
  33. | 1 || 0 |
  34. +--------+
  35. | 1 |
  36. +--------+
  37. mysql> select not 1;
  38. +-------+
  39. | not 1 |
  40. +-------+
  41. | 0 |
  42. +-------+
  43. mysql> select !0;
  44. +----+
  45. | !0 |
  46. +----+
  47. | 1 |
  48. +----+
  49. ## 异或
  50. mysql> select 1 xor 1;
  51. +---------+
  52. | 1 xor 1 |
  53. +---------+
  54. | 0 |
  55. +---------+
  56. mysql> select 0 xor 0;
  57. +---------+
  58. | 0 xor 0 |
  59. +---------+
  60. | 0 |
  61. +---------+
  62. mysql> select 1 xor 0;
  63. +---------+
  64. | 1 xor 0 |
  65. +---------+
  66. | 1 |
  67. +---------+
  68. mysql> select null or 1;
  69. +-----------+
  70. | null or 1 |
  71. +-----------+
  72. | 1 |
  73. +-----------+
  74. mysql> select 1 ^ 0;
  75. +-------+
  76. | 1 ^ 0 |
  77. +-------+
  78. | 1 |
  79. +-------+

运算符优先级

最低优先级为: :=
Mysql 运算符 - 图1
最高优先级为: !BINARYCOLLATE

位运算


https://www.runoob.com/mysql/mysql-operator.html