Tuesday, 6 October 2015

FIND_IN_SET() Function in MySql

How to use FIND_IN_SET() function in MySql . This tutorial explain how to use this in-built function to search for values within a comma separated values.
While working on some module i came across with MySql FIND_IN_SET() function. In my case i have to pull all the records whose status match with status list separated with comma are stored in table.

Syntax of FIND_IN_SET() function

argument1 is a string.
argument2 is a string list separated by comma.
find_in_set() function returns the position of a string within second string. Returns zero when search string doesn’t exist in string list.
Note:
i) If string is not found in string_list, the FIND_IN_SET function will return 0.
ii) If string is NULL, the FIND_IN_SET function will return NULL.
iii) If string_list is an empty string, the FIND_IN_SET function will return 0.
iv) If string_list is NULL, the FIND_IN_SET function will return NULL.
Examples

Explanation of find_in_set() Function through Example

Create one table category and populate some values.
Table will look like this.

Let’s write query to find all the category_id and name whose cat_path contains value 4.
Three matching rows are fetched.

0 comments:

Post a Comment