×

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 COUNT function

The count function returns the number of rows in the table. This function shows either the entire rows count or the required row count of the table. It determines the...

5 minutes read.

MySQL Show index

This query helps to access and retrieve table information. This syntax displays the required index or all indexes of the table. It also displays the index type as per the...

3 minutes read.

MySQL SUM function

The SUM() function displays the total addition of the table values. It supports the arithmetic operation of the column values. This function does not return a null value. Here, the...

5 minutes read.

MySQL Table

Introduction MySQL table is an essential part of the system. MySQL table stores data using index, rows, and columns. It accesses data from the server quickly because of the table—this table...

22 minutes read.

MySQL Drop Index

Sometimes we have an index that is not required in data operations. In that case, we can use this statement to remove an existing index from the table. We can...

4 minutes read.

MySQL: Entity-Relationship Model

Entity-Relationship Model Entity-Relationship model or E R model is used to create a relationship between different attributes or entities. It describes the structure of the database with the help of the...

5 minutes read.

MySQL ELT() function

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

2 minutes read.

MySQL CROSS JOIN

MySQL CROSS JOIN combines all possibilities of the two or more tables and returns the result that contains every row from all contributing tables. It links several columns from the...

4 minutes read.

MySQL RPAD() function

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

2 minutes read.

MySQL EQUI JOIN

An equijoin is an operation that combines multiple tables based on equality or matching column values in the associated tables. This operation links more than two tables based on a...

5 minutes read.

MySQL PERIOD_DIFF() function

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

2 minutes read.

MySQL MOD() function

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

3 minutes read.

MySQL INSTR() Function

In this context, we will learn how we can use the MySQL INSTR() function with proper syntax and good examples. Introduction of MySQL INSTR() function This function in MySQL returns the location...

4 minutes read.

MySQL Prefix index

Prefix index This index query creates an index column in the string or character column. MySQL system can create multiple indexes in the table. It will create in a table as...

3 minutes read.

MySQL FLOOR() function

In this context, we will learn how we can use the MySQL FLOOR() function with proper syntaxes and examples. Introduction of MySQL Floor() function: FLOOR() function in MySQL is used to return...

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 Aggregate function

The aggregation function works on the multiple values and returns single value output.  It makes the advanced and complex operation simple. The summation (SUM), MAX, AVG, MIN, COUNT functions are...

5 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.

MySQL logical conditions

MySQL logical conditions Introduction MySQL handles data with clauses, operators, and conditions. The logical condition is used to compare information and returns the required output. This condition applies logic to MySQL expressions...

7 minutes read.

MySQL DATE_FORMAT() function

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

3 minutes read.