Select*fromStudent;+------+---------+--- ------+-----------+|Id |Name |Address|Subject |+------+-----"/> Select*fromStudent;+------+---------+--- ------+-----------+|Id |Name |Address|Subject |+------+-----">

Home >Database >Mysql Tutorial >How can we use MySQL INSTR() function with WHERE clause?

How can we use MySQL INSTR() function with WHERE clause?

WBOY
WBOYforward
2023-09-04 11:41:061006browse

我们如何将 MySQL INSTR() 函数与 WHERE 子句一起使用?

When we use the INSTR() function with the MySQL WHERE clause, we need to provide the column name of the table as the first parameter and the substring as the second parameter and comparison operators. Below is an example for demonstration using 'Students' table -

Example

Suppose there are following values ​​in 'Students' table-

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)

Now, the following query shows how to INSTR() function used with WHERE caluse-

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)

The above is the detailed content of How can we use MySQL INSTR() function with WHERE clause?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete