MySQL 8 Cookbook
上QQ阅读APP看书,第一时间看更新

Operators

MySQL supports many operators for filtering results. Refer to https://dev.mysql.com/doc/refman/8.0/en/comparison-operators.html for a list of all the operators. We will discuss a few operators here. LIKE and RLIKE are explained in detail in the next examples:

  • Equality: Refer to the preceding example where you have filtered using =.
  • IN: Check whether a value is within a set of values.
    For example, find the count of all employees whose last name is either ChristLamba, or Baba:
mysql> SELECT COUNT(*) FROM employees WHERE last_name IN ('Christ', 'Lamba', 'Baba');
+----------+
| COUNT(*) |
+----------+
| 626 |
+----------+
1 row in set (0.08 sec)
  • BETWEEN...AND: Check whether a value is within a range of values.
    For example, find the number of employees who were hired in December 1986:
mysql> SELECT COUNT(*) FROM employees WHERE hire_date BETWEEN '1986-12-01' AND '1986-12-31';
+----------+
| COUNT(*) |
+----------+
| 3081 |
+----------+
1 row in set (0.06 sec)
  • NOT: You can simply negate the results by preceding with the NOT operator.
    For example, find the number of employees who were NOT hired in December 1986:
mysql> SELECT COUNT(*) FROM employees WHERE hire_date NOT BETWEEN '1986-12-01' AND '1986-12-31';
+----------+
| COUNT(*) |
+----------+
| 296943 |
+----------+
1 row in set (0.08 sec)