×

MySQL FIND_IN_SET() 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

MySQL provides a built-in string function called FIND_IN_SET() that allows you to find the position of a string within a comma-separated list of strings.

FIND_IN_SET() function is used to find the position of a string within a list of strings. If the string is repeated multiple times, the output will be the first position of that string.

Following are the Some important Points of MySQL FIND IN SET function:

1. If the string is not found in string_list, the result is 0.

2. If string or string_list is NULL, the result is NULL.

3. If string_list is an empty string (""), the result is 0.

Syntax of the MySQL MOD() function

The syntax of the MySQL MOD() function is given as follows:

FIND_IN_SET("string", "string_list")

Parameters or arguments used in MySQL MOD() function:

There are two parameters accepted by the MOD() function in MySQL, which are given as follows:

The first parameter string is the string that you want to find.The second parameter, string_list, is a list of comma-separated strings that are to be searched.

The FIND_IN_SET() function returns an integer or a NULL value depending on the value of the arguments:

  • Return a NULL value if either string or string_list is NULL.
  • Return zero if the string is not in the string_list or the string_list is an empty string.
  • Return a positive integer if the string is in the string_list.

Note: Parameter string is mandatory to search for string_list; string_list is a list of string values.

Application used for MOD() function:

The MOD() function can be used in the given below MySQL versions.:

  • MySQL 5.7
  • MySQL 5.6
  • MySQL 5.5
  • MySQL 5.1
  • MySQL 5.0
  • MySQL 4.1
  • MySQL 4.0
  • MySQL 3.23

Examples of MySQL MOD() function:

Now we will look into some MySQL MOD() function examples and will explore how we can use the MOD function in MySQL.

Example-1:

In this example, we will search for "b" within the list of strings:

SELECT FIND_IN_SET("b," "j, a, v, a, t, p, o, i, n, t, s");

Output:

FIND_IN_SET("b," "javatpoints")
0

Example-2:

In this example, we will search for "q" within the list of strings (string list is NULL):

SELECT FIND_IN_SET("a", null);

Output: –

FIND_IN_SET(“a,” null)
null

Example-3:

In this example, we will search for "q" within the list of strings:

SELECT FIND_IN_SET("g", "g, e, e, k, s, f, o, r, g, e, e, k, s");

Output: –

FIND_IN_SET(“g”, “g, e, e, k, s, f, o, r, g, e, e, k, s”)
1

Some Simple Examples of Find_In_Set function:

Example A: The following statement returns 2 because y has the second position in the 'x,y,z' string:

SELECT FIND_IN_SET('y','x,y,z');

Output:

2

Example B: The following statement returns 0 because a is not in the 'x,y,z' list:

SELECT FIND_IN_SET('a','x,y,z');

Output:

0

Example C: The following statement also returns 0 because the second argument is empty:

SELECT FIND_IN_SET('a','');

Output:

0

Example D: The following statement returns NULL because the first argument is NULL:

SELECT FIND_IN_SET(NULL,'x,y,z');

Output:

NULL

Example E: The following statement also returns NULL because the second argument is NULL:

SELECT FIND_IN_SET('a',NULL);

Output:

NULL

Example of Find_In_Set Function with Table

First, we will make a new table named segment using the following statement.

CREATE TABLE IF NOT EXISTS segment (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(25) NOT NULL,
belts VARCHAR(200) NOT NULL
);

After that, we will add some rows to the segment table.

INSERT INTO segment(name,belts)
VALUES ('O-1', 'white, yellow, orange'),
('O-2', 'purple, green, blue'),
('O-3', 'brown, red, black'),
('O-4', 'white, yellow, orange'),
('O-5', 'purple, green, blue'),
('O-6', 'brown, red'),
('O-7', 'black'),
('O-8', 'white, yellow, orange'),
('O-9', 'purple, green, blue'),
('O-10', 'brown, red');

Then, we can use the FIND_IN_SET() function to find the segment that accepts the red belt, as shown in the following query:

SELECT
name,
belts
FROM
segment
WHERE
FIND_IN_SET('red', belts);

Output:

Name  Belts  
0-3  Red, brown, black  
0-6  Red, brown
0-10  Red, brown  

FIND_IN_SET() function with NOT operator

The FIND_IN_SET() function returns zero, which is FALSE in MySQL when the first argument is not found in the second argument. Therefore, you can use the NOT operator to negate the FIND_IN_SET() function.

This example uses the NOT operator with the FIND_IN_SET() function to find the segment that does not accept the black belt:

SELECT
name, belts
FROM
segment
WHERE
NOT FIND_IN_SET('black', belts);

MySQL FIND_IN_SET() function VS IN operator

The IN operator determines whether a value matches any value in a set. The following example uses the IN operator to find the segment whose name is O-1 or O-2:

SELECT
name, belts
FROM
segment
WHERE
name IN ('O-1' , 'O-2');

This statement uses the FIND_IN_SET() function and returns the same result as the above query:

SELECT
name,
belts
FROM
segment
WHERE
FIND_IN_SET(name, 'O-1,O-2');

The IN operator can take any number of arguments, each separated by a comma, while the FIND_IN_SET() function can take only two arguments.

You use the IN operator when you want to match a value with any value in a list. And you can use the FIND_IN_SET() function when you want to match a value with the specified list of values.

Summary:

In this tutorial, you have learned how to use the MySQL FIND_IN_SET() function to find a string in a comma-separated list of strings.


Related Topics

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 RTRIM() function

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

2 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 INSERT() Function

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

2 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 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 MIN function

The MIN() function determines the minimum or lowest value of the data set. This function works on numerical data type values. If the table displays zero value, then the row...

4 minutes read.

MySQL DAY() function

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

2 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 Tutorial

In this tutorial, you will get information about the MySQL management system. This tutorial will cover the basic and advanced level MySQL concepts with examples that will help you become...

5 minutes read.

MySQL NOW() function

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

2 minutes read.

MySQL database queries

MySQL database queries Database queries help to create, use, show and, delete the database. A Database is a collection of several tables and data. The database uses SQL language to perform...

3 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 Composite Index

Composite Index A composite index is an index that is used on multiple columns. MySQL management system manages multiple columns simultaneously. Therefore, the single index contains multiple columns in one query...

3 minutes read.

MySQL LEAST function

This statement displays the smallest value of the table. The LEAST function returns a null value when the table contains a null value. If the table contains all numerical values,...

2 minutes read.

MySQL LOG10() function

In this context, we will learn how we can use the MySQL LOG10() function with proper syntax and good examples. Introduction of MySQL LOG10() function To evaluate the natural logarithmic value of...

2 minutes read.

MySQL Full text index

Full text index The full-text index in the table isused to search the full text of the data. This index assigns to the table using a "FULLTEXT" keyword. First, the table...

3 minutes read.

MySQL LOCATE() function

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

3 minutes read.

MySQL Date and Time function

The date function displays day, year, month, time, and current date. It shows the date and time as per the requirement of the applications. The data either store on the...

8 minutes read.

MySQL Invisible Index

Invisible Index MySQL index decides an option to the availability of the index for the query optimizer. This index shows the visible and invisible options to displays the index column in...

3 minutes read.