
上QQ阅读APP看书,第一时间看更新
Simple pattern matching
You can use the LIKE operator. Use underscore (_) for matching exactly one character. Use % for matching any number of characters.
- Find the count of all employees whose first name starts with Christ:
mysql> SELECT COUNT(*) FROM employees WHERE first_name LIKE 'christ%';
+----------+
| COUNT(*) |
+----------+
| 1157 |
+----------+
1 row in set (0.06 sec)
- Find the count of all employees whose first name starts with Christ and ends with ed:
mysql> SELECT COUNT(*) FROM employees WHERE first_name LIKE 'christ%ed';
+----------+
| COUNT(*) |
+----------+
| 228 |
+----------+
1 row in set (0.06 sec)
- Find the count of all employees whose first name contains sri:
mysql> SELECT COUNT(*) FROM employees WHERE first_name LIKE '%sri%';
+----------+
| COUNT(*) |
+----------+
| 253 |
+----------+
1 row in set (0.08 sec)
- Find the count of all employees whose first name ends with er:
mysql> SELECT COUNT(*) FROM employees WHERE first_name LIKE '%er';
+----------+
| COUNT(*) |
+----------+
| 5388 |
+----------+
1 row in set (0.08 sec)
- Find the count of all employees whose first name starts with any two characters followed by ka and then followed by any number of characters:
mysql> SELECT COUNT(*) FROM employees WHERE first_name LIKE '__ka%';
+----------+
| COUNT(*) |
+----------+
| 1918 |
+----------+
1 row in set (0.06 sec)