Difference between IN and FIND_IN_SET in MySQL
- Digital Engineering
- General
Difference between IN and FIND_IN_SET in MySQL
Most of people thinks IN() and FIND_IN_SET() MySQL functions are identical and do the same job.
But they not. You may be confused with them or don’t know when to use IN() or FIND_IN_SET().There are specific situation for both MySQL functions.
To understand it better assume a table user
Table:user
userid | name | age | fav_color |
1 | Hitesh Shrimali | 29 | Green, Blue |
2 | Jeetendra Sharma | 31 | Blue, Pink |
3 | Ram Singh | 29 | Red, Blue |
-
IN() mysql Function
This function is used when we want to compare field (which contain single value) with list of values in database.
1 |
Example – Select * From user Where userid IN (1, 2); |
Result – query will return 2 rows with userid 1 and 2.
-
FIND_IN_SET() mysql function
This function is used when any field holds comma separated values in it and we want to compare it with single value.
1 |
Example – Select * From user Where FIND_IN_SET(“green”, fav_color); |
Result – query will return 1 row with userid 1.
I hope these will clear your confusion and help you to decide when to use one of them.
Related content
Auriga: Leveling Up for Enterprise Growth!
Auriga’s journey began in 2010 crafting products for India’s