
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
What is the difference between MySQL LOCATE() and FIND_IN_SET() functions?
As we know, both the functions are used to search a string from the arguments provided in them but there are some significant differences between them as follows
- FIND_IN_SET() function uses the string list that is itself a string containing the substring separated by commas. Whereas, LOCATE() function contains a string from which it will find the position of the first occurrence of the substring if present.
- In LOCATE() function we can manage the starting point of the search by providing an optional argument for a position. Whereas, for FIND_IN_SET() function MySQL do not provide such kind of flexibility and the search would by default starting from 1st string.
- In case of integers, FIND_IN_SET() is much more suitable than LOCATE() function. It can be understood by the following example
Example
mysql> Select IF(LOCATE(2,'10,11,12,13') > 0,1,0) As result; +--------+ | result | +--------+ | 1 | +--------+ 1 row in set (0.05 sec) mysql> Select IF(FIND_IN_SET(2,'10,11,12,13') > 0,1,0)As Result; +--------+ | Result | +--------+ | 0 | +--------+ 1 row in set (0.00 sec)
From the result set of above examples, we can see that LOCATE() function returns 1 even 2 as a string is not present in the arguments. But FIND_IN_SET() function returns 0 which is correct answer.
- Related Articles
- What is the difference between MySQL INSTR() and FIND_IN_SET() functions?
- How MySQL LOCATE() function is different from its synonym functions i.e. POSITION() and INSTR() functions?
- What is the use of FIND_IN_SET () function in MySQL?
- What is the difference between CONCAT() and CONCAT_WS() functions?
- What is the difference between functions and methods in JavaScript?
- What is the difference between Python functions datetime.now() and datetime.today()?
- Which MySQL functions work as similar as LOCATE() function?
- What is the difference between anonymous and inline functions in JavaScript?
- What is the difference between jQuery.map() and jQuery.grep() Functions in jQuery?
- What is the difference between jQuery.map() and jQuery.each() Functions in jQuery?
- What is the difference between ajaxSend() and ajaxStart() functions in jQuery?
- What is the difference between ajaxStop() and ajaxComplete() functions in jQuery?
- What is the difference between ajaxSuccess() and ajaxComplete() functions in jQuery?
- What is the difference between closure and nested functions in JavaScript?
- What is the difference between virtual and abstract functions in C#?

Advertisements