Thursday, 8 July 2021

MYSQL FIND_IN_SET() usage and Laravel implication

 FIND_IN_SET used in select

https://www.w3schools.com/sql/func_mysql_find_in_set.asp

The FIND_IN_SET() function returns the position of a string within a list of strings.



  • If string is not found in string_list, this function returns 0
  • If string or string_list is NULL, this function returns NULL
  • If string_list is an empty string (""), this function returns 0
FIND_IN_SET("a", "s,q,l")
0


FIND_IN_SET used in where
https://stackoverflow.com/questions/16209040/mysql-find-in-set-in-where-clause

*Caution, this will cause a full table scan, should be using column IN (value1,value2)
SELECT * FROM employee WHERE FIND_IN_SET(1,comma_separated_column_name)

Here is my example enter image description here

The query selects all trips that have 1 in the weekend column


Laravel usage

https://stackoverflow.com/questions/35594450/find-in-set-in-laravel-example

$colname = 'css'
$query = DB::table('tags_value')
         ->whereRaw('FIND_IN_SET(?,Tags)', [$colname])
         ->get();




No comments:

Post a Comment