如何在 MySQL INSTR() 函式中與 WHERE 子句結合使用?
當我們把 INSTR() 函式與 MySQL WHERE 子句結合使用時,我們需要把表的列名作為第一個引數,把子字串作為第二個引數,並加上一個比較運算子。下面是一個使用 'Student' 表進行演示的示例 −
示例
假設我們在 '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 rows in set (0.02 sec)
現在,下面的查詢演示瞭如何將 INSTR() 函式與 WHERE 子句結合使用 −
mysql> select name, INSTR(Name,'av')As Result from student where INSTR(Name,'av') > 0; +--------+--------+ | name | Result | +--------+--------+ | Gaurav | 5 | | Aarav | 4 | | Gaurav | 5 | +--------+--------+ 3 rows in set (0.00 sec) mysql> select name, INSTR(Name,'av')As Result from student where INSTR(Name,'av') = 0 ; +---------+--------+ | name | Result | +---------+--------+ | Harshit | 0 | | Yashraj | 0 | +---------+--------+ 2 rows in set (0.01 sec)
廣告