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
Release: 2023-09-04 11:41:06
forward
1050 people have browsed it

我们如何将 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)
Copy after login

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)
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template