MySQL Tutorial

MySQL Tutorial MySQL Features MySQL Database Introduction MySQL Environmental Setup MySQL Data Types MySQL variable MySQL Advance table Query MySQL database queries MySQL Entity-Relationship Model MySQL Table Query MySQL Operators MySQL logical conditions MySQL Queries MySQL Clauses Clustered vs Non-Clustered Index MySQL Full text index MySQL Descending Index MySQL Invisible Index MySQL Composite Index MySQL Prefix index MySQL Index MySQL Create index MySQL Drop Index MySQL Show index MySQL Unique index MySQL Table MySQL Variable MySQL View MySQL Constraints MySQL Command Line Client Basic Queries MySQL Stored Procedure MySQL IF Statement MySQL Subquery MySQL Triggers

MySQL Join

MySQL Join MySQL CROSS JOIN MySQL DELETE JOIN MySQL EQUI JOIN MySQL INNER JOIN MySQL Union MySQL NATURAL JOIN MySQL RIGHT JOIN MySQL SELF JOIN MySQL UPDATE JOIN

MySQL Function

MySQL Function MySQL AVG() Function MySQL SUM() Function MySQL String() Function MySQL Advance() Function MySQL Aggregate() Function MySQL COALESCE() Function MySQL Control Flow Function MySQL COUNT() Function MySQL Date And Time Function MySQL GREATEST() Function MySQL ISNULL() Function MySQL LEAST() Function MySQL Math() Function MySQL MAX() Function MySQL MIN() Function MySQL find_in_set() function MySQL ASIN() Function MySQL CEIL() function MySQL CEILING() function MySQL TAN() Function MySQL Truncate() Function MySQL FLOOR() function MySQL LN() function MySQL LOG2() function MySQL LOG10() function MySQL MOD() function MySQL PI() function MySQL POW() function MySQL RADIANS() function MySQL RAND() function MySQL ROUND() function MySQL Character Length Function MySQL Current Date Function MySQL Date Add Function MySQL Date Format Function MySQL Datediff Function MySQL Day Function MySQL Elt Function MySQL Export Set Function MySQL Field Function MySQL Format Function MySQL From Base64 Function MySQL Hex Function MySQL Insert Function MySQL Instr Function MySQL Length Function MySQL CONCAT() function MySQL FIND_IN_SET() function MySQL LIKE() function MySQL LOAD_FILE() function MySQL LOCATE() function MySQL LOG() function MySQL MONTHNAME() function MySQL NOW() function MySQL PERIOD_ADD() function MySQL PERIOD_DIFF() function MySQL POWER() function MySQL QUARTER() function MySQL REVERSE() function MySQL RIGHT() Function MySQL RPAD() function MySQL RTRIM() function MySQL SEC_TO_TIME() function MySQL SOUNDEX() function

Questions

Which Clause is Similar to Having Clause in MySQL

Misc

MySQL Error 1046 - No Database Selected Failed to Start MySQL Service Unit MySQL Service Unit not Found Import MySQL Connector Mudule not Found Error No Module Named MySQL Joins Available in MySQL MySQL Docs MySQL Download For Windows 7 64 Bit MySQL Error Code 1064 MySQL Export MySQL History MySQL Host MySQL Import MySQL Drop All Tables MySQL Drop MySQL Error Code 1175 MySQL Events MySQL Except MYSQL Foreign Key Constraint MySQL If Exists MySQL IndexOf MySQL List All Tables json_extract in MySQL TIMESTAMPDIFF in MySQL MySQL Syntax Checker Sudo MySQL Secure Installation

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.