Home > Database > Mysql Tutorial > body text

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

WBOY
Release: 2023-08-26 19:09:09
forward
751 people have browsed it

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

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

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

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!

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