×

find_in_set() function in MySQL

This Function is used for finding the position of a particular string from the list of strings. For suppose if the specified string is repeated multiple times, then this functions outputs the first occurrence of that specified string.

We provide the specified string and the list of strings to the function as the parameters.

  • If the specified string is absent in the list of strings, then this function returns 0.
  • If the specified string is NULL, then this function will result a NULL value.
  • If the List of strings is NULL or an empty string, then this function will return 0.

Syntax:

FIND_IN_SET (“String”, “List_of_Strings”)

Parameters:

  • String: This is the string which should be searched in the list of strings.
  • List_of_Strings: This is the strings list from which the searching is done.

Example1:

Search for a “e” from the Strings list.

select FIND_IN_SET (“e”, “j, a, v, a, T, p, o, i, n, t”);

Output:

FIND_IN_SET (“e”, “javaTpoint”);
0

As the letter “e” is not present in the list of strings, this function will give 0 as output.

Example2:

Now, we will search for “T” in “javaTpoint”.

select FIND_IN_SET (“T”, “j, a, v, a, T, p, o, i, n, t”);

Output:

FIND_IN_SET (“T”, “javaTpoint”);
5

As the letter “T” is present in the list of strings at the position 5, this function will give 5 as output.

Example3:

Now, let’s search for “a” in “javaTpoint”.

select FIND_IN_SET (“a”, “j, a, v, a, T, p, o, i, n, t”);

Output:

FIND_IN_SET (“a”, “javaTpoint”);
2

As letter “a” is present twice in the list of strings (i.e “javaTpoint”), this function will give 2 as output. It gives the first occurrence as the output. In “javaTpoint”, the letter “a” is present in two positions, 2nd and 4th position. So, we got the output as 2.

Example4:

select FIND_IN_SET (“z”, null);
FIND_IN_SET (“z”, null);
null

Example5:

select FIND_IN_SET (“t”, “j, a, v, a, T, p, o, i, n, t);

Output:   

FIND_IN_SET (“t”, “j, a, v, a, T, p, o, i, n, t);
10

As “t” is at 10th position, this function results 10 as the output.

Example6:

FIND_IN_SET () Function using where clause.

Consider a Table AUTHOR having ID, Name, country as the fields.

create table AUTHOR (ID number, Name varchar (20), country varchar (25));

>Table Created.

After inserting rows into the table, we are having the following table.

select * from AUTHOR;
IDNameCountry
AUT011William AnthonyGermany
AUT012Thomas MorganUK
AUT013Nikolai DeweyCanada
AUT014William NortonBrazil
AUT015William MaughamUSA
AUT016Joseph MiltonCanada
AUTO17Evan HayekAustralia
AUTO18Joseph DeweyUSA
AUTO19William MorganAustralia

>9 rows selected.

Now, if we execute the following query,

select * from author where FIND_IN_SET (left (Name,7), “William, steven, angalo, Jung”) > 0;

Output:

IDNameCountry
AUT011William AnthonyGermany
AUT014William NortonBrazil
AUT015William MaughamUSA
AUTO19William MorganAustralia

The above query will give the rows having author name Starting with “William”.

In this way we can use the FIND_IN_SET Function in where clause also. In this way we use the FIND_IN_SET Function in the database applications.

Let’s look into some examples using tables from the database.

create table if not exists Divisions (id number AUTO_INCREMENT primary key, name varchar (20) not null, belts varchar (20));

Now, we should insert some rows into the table Divisions.

insert into Division values (‘0-1’, ‘yellow, red, purple’);
insert into Division values (‘0-2’, ‘brown, black, brown’);
insert into Division values (‘0-3’, ‘white, green, purple’);
insert into Division values (‘0-4’, ‘blue, yellow, pink’);
insert into Division values (‘0-5’, ‘blue, red, pink’);


insert into Division values (‘0-6’, ‘red, yellow, pink’);
insert into Division values (‘0-7’, ‘blue, yellow, red’);
insert into Division values (‘0-8’, ‘blue, pink, purple’);
insert into Division values (‘0-9’, ‘green, blue, pink’);
insert into Division values (‘0-10’, ‘blue, purple, pink’);
select * from Divisions;
IDNameBelts
10-1Yellow, red, purple
20-2brown, black, brown
30-3White, green, purple
40-4Blue, yellow, pink
50-5blue, red, pink
60-6red, yellow, pink
70-7blue, yellow, red
80-8blue, pink, purple
90-9green, blue, pink
100-10blue, purple, pink

>10 rows selected.

To, find a particular division having a red belt, we use Find_in_Set() Function in the query as follows:

select id, name, belts from Divisions where find_in_set (‘purple’, belts);

Output:

IDNameBelts
10-1Yellow, red, purple
30-3White, green, purple
80-8blue, pink, purple

Negative Find_In_Set() Function:

To, print the argument which is not in the second argument, we should use NOT operator. Consider the following query,

select id, name, belts from Divisions where NOT FIND_IN_SET (‘white’, belts);

Output:

IDNameBelts
10-1Yellow, red, purple
20-2Brown, black, brown
40-4Blue, yellow, pink
50-5blue, red, pink
60-6red, yellow, pink
70-7blue, yellow, red
80-8blue, pink, purple
90-9green, blue, pink
100-10blue, purple, pink

FIND_IN_SET () Function vs. IN operator:

The IN operator works whether a value matched in a set or not. Now, we will look into an example having IN operator in the query,

select id, name, belts from Divisions where name IN (‘0-1’, ‘0-3’);

IDNameBelts
10-1Yellow, red, purple
20-3White, green, purple

The result which is produced in the above query, can be obtained using FIND_IN_SET () Function also. For that we write the below query,

select id, name, belts from Division where FIND_IN_SET (name, ‘0-1, 0-3’);

IDNameBelts
10-1Yellow, red, purple
20-3White, green, purple

The IN operator can take many arguments with in the function whereas the Find_In_Set () Function can only take two arguments.

Generally, we use the IN operator if we want to match a value with any value from the provided list.

We use Find_In_Set () Function to match the given value with the comma separated list of values.

In this way we can differentiate the Find In Set Function and IN operator.


Related Topics

MySQL DELETE JOIN

Sometimes join becomes complicated and unwanted. In such cases, we can delete the unwanted joins in the tables. You can delete inner join, left join, right join as per requirement....

3 minutes read.

MySQL POWER() function

In this context, we will learn how we can use the MySQL POWER() function with proper syntax and good examples. Introduction of MySQL POWER() function The value of a number raised to...

3 minutes read.

MySQL EXPORT_SET() function

In this context, we will learn how we can use the MySQL EXPORT_SET() function with proper syntax and good examples. Introduction of MySQL EXPORT_SET() function This function returns a string and shows...

3 minutes read.

MySQL Vs MongoDB

What is MongoDB? MongoDB is a distributed, open-source, cross-platform document-based database which was created to scale and develop applications simply. It was created as a NoSQL database by MongoDB Inc. MongoDB gets...

6 minutes read.

MySQL TAN() Function

Basically, the TAN() function in MySQL is used to give back the tangent of a mentioned number.In any kind of right-angle triangle, the tangent of an angle is the length...

2 minutes read.

MySQL SOUNDEX() function

In this context, we will learn how we can use the MySQL SOUNDEX() function with proper syntax and good examples. Introduction of MySQL SOUNDEX() function SOUNDEX() function in MySQL is used to...

3 minutes read.

MySQL LOAD_FILE() function

In this context, we will learn how we can use the MySQL LOAD_FILE() function with proper syntax and good examples. Introduction of MySQL LOAD_FILE() function The LOAD_FILE() function in MySQL reads a...

3 minutes read.

MySQL REVERSE() function

In this context, we will learn how we can use the MySQL REVERSE() function with proper syntax and good examples. Introduction of MySQL REVERSE() function This function could be used to reverse...

2 minutes read.

MySQL LN() function

In this context, we will learn how we can use the MySQL LN() function with proper syntax and good examples. Introduction of MySQL LN() function For evaluation of the natural logarithm of...

2 minutes read.

MySQL Create index

An index creation helps to make a row of the table unique. The index operates and handles table data quickly. MySQL index requires NOT NULL column constraint. The index column...

2 minutes read.

MySQL DATEDIFF() function

In this context, we will learn how we can use the MySQL DATEDIFF() function with proper syntax and good examples. Introduction of MySQL DATEDIFF() function DATEDIFF() function in MySQL is used to...

3 minutes read.

MySQL DATE_ADD() function

In this context, we will learn how we can use the MySQL DATE_ADD() function with proper syntax and good examples. Introduction of MySQL DATE_ADD() function In MySQL, the DATE_ADD() function is used...

3 minutes read.

MySQL LENGTH() Function

In this context, we will learn how we can use the MySQL LENGTH() function with proper syntax and good examples. Introduction of MySQL LENGTH() function This function in MySQL is used to...

2 minutes read.

MySQL View

Introduction MySQL View is a virtual table to create a clone of the base table. The View does not contain its values or data. MySQL View creates to connect more than...

12 minutes read.

MySQL Descending Index

Descending Index It is a type of index that stores key values in descending order. This query improves the performance of an index query. MySQL system displays the index as per...

3 minutes read.

MySQL variable

The MySQL variable is essential for storing data in the table. The variable declares data with a specific name or label to avoid confusion. This data label is used in...

6 minutes read.

MySQL Queries

MySQL Queries MySQL supports SQL queries in the MySQL interface. These queries help to interact data with the application. MySQL uses create database, user database, create a table, truncate table, and...

13 minutes read.

MySQL CONCAT() function

In this context, we will learn how we can use the MySQL CONCAT() function with proper syntax and good examples. Introduction of MySQL CONCAT() function By using the MySQL CONCAT function, we...

3 minutes read.

MySQL Triggers

MySQL trigger is a function of the stored procedure to respond to the system program. This function responds and runs any data table event automatically. You can use it for...

5 minutes read.

find_in_set() function in MySQL

This Function is used for finding the position of a particular string from the list of strings. For suppose if the specified string is repeated multiple times, then this functions...

4 minutes read.