Select*fromwebsites;+----+---------------+--- ---------------------+|Id|Purpose |Webaddress &"/> Select*fromwebsites;+----+---------------+--- ---------------------+|Id|Purpose |Webaddress &">

Home  >  Article  >  Database  >  How do we find the index position of a string stored as a record in a MySQL table column?

How do we find the index position of a string stored as a record in a MySQL table column?

WBOY
WBOYforward
2023-08-26 19:09:09712browse

How do we find the index position of a string stored as a record in a MySQL table column?

We can use the FIELD() function to find the index location of a string stored as a record in a MySQL table column. To demonstrate it, we are using a table named "websites" which contains the following data

Example

mysql> Select * from websites;
+----+---------------+------------------------+
| Id | Purpose       | Webaddress             |
+----+---------------+------------------------+
| 1  | For tutorials | www.tutorialspoint.com |
| 2  | For searching | www.google.co.in       |
| 3  | For email     | www.gmail.com          |
+----+---------------+------------------------+
3 rows in set (0.00 sec)

Now, let's say we want to retrieve the data from "Usage" and "URL" stored in this table To find the index number of a specific string (such as "email") from the string in the record in the column, the following content query is enough -

mysql> Select FIELD('For email', purpose, webaddress) From websites;
+----------------------------------------+
| FIELD('For email', purpose, webaddress)|
+----------------------------------------+
|                                      0 |
|                                      0 |
|                                      1 |
+----------------------------------------+
3 rows in set (0.00 sec)

The above result set displays the "For email" character The string is located at the first index of the third row.

mysql> Select FIELD('www.tutorialspoint.com', purpose, web address) From websites;
+------------------------------------------------------+
| FIELD('www.tutorialspoint.com', purpose, web address)|
+------------------------------------------------------+
|                                                    2 |
|                                                    0 |
|                                                    0 |
+------------------------------------------------------+
3 rows in set (0.00 sec)

The above result set shows that the "www.tutorialspoint.com" string is located at the second index of the first row.

The above is the detailed content of How do we find the index position of a string stored as a record in a MySQL table column?. 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