MySQL 如何在MySQL中使用带WHERE子句的INSTR()函数?
当我们在MySQL WHERE子句中使用INSTR()函数时,需要将表的列名作为第一个参数,将子字符串作为第二个参数,以及比较运算符。下面是使用“Student”表演示它的示例−
阅读更多:MySQL 教程
示例
假设我们在“Student”表中有以下值−
mysql> Select * from Student;
+------+---------+---------+-----------+
| Id | Name | Address | Subject |
+------+---------+---------+-----------+
| 1 | Gaurav | Delhi | Computers |
| 2 | Aarav | Mumbai | History |
| 15 | Harshit | Delhi | Commerce |
| 20 | Gaurav | Jaipur | Computers |
| 21 | Yashraj | NULL | Math |
+------+---------+---------+-----------+
在集合中有5行(0.02秒)
现在,以下查询显示了我们如何在WHERE子句中使用INSTR()函数−
mysql> select name, INSTR(Name,'av')As Result from student where INSTR(Name,'av') > 0;
+--------+--------+
| name | Result |
+--------+--------+
| Gaurav | 5 |
| Aarav | 4 |
| Gaurav | 5 |
+--------+--------+
在集合中有3行(0.00秒)
mysql> select name, INSTR(Name,'av')As Result from student where INSTR(Name,'av') = 0 ;
+---------+--------+
| name | Result |
+---------+--------+
| Harshit | 0 |
| Yashraj | 0 |
+---------+--------+
在集合中有2行(0.01秒)
极客教程