Questions tagged [find-in-set]

MySQL FIND_IN_SET() returns the position of a string if it is present (as a substring) within a list of strings.

MySQL FIND_IN_SET(str, strlist) is a string function that returns the index position of the search string str inside the string list strlist. The string list itself is a string containing substrings separated by ‘,’ (comma) character.

This function returns 0 when search string does not exist in the string list and returns NULL if either of the arguments is NULL.

Syntax

FIND_IN_SET(search_string, string_list)

Arguments

  • search_string: a string which is to be looked for in following list of arguments.
  • string_list: list of strings to be searched if they contain the search string.

Example

The following MySQL statement finds the search string ‘ank’ at 2nd place within the string list. So it returns 2.

Code

SELECT FIND_IN_SET('ank','b,ank,of,monk');

Output

FIND_IN_SET('ank','b,ank,of,monk')
2
149 questions
0
votes
2 answers

MySQL ORDER By one Specific value in column (having comma separated values)

I want to sort the user record according to city (chosen from the drop-down list). like if I pass city_id 22 in my query then i want all the row first which are having city_ids 22 then the rest of the rows. I know WHERE find_in_set('22',city_ids)…
amit gupta
  • 1,282
  • 2
  • 20
  • 36
0
votes
0 answers

MariaDB select record for every find_in_set values

I'm pretty sure there are already topics on this subject. I just can't find them. Sorry... I want to select rows from a table where column = 1 or 2, or 3, or 3. I need to use FIND_IN_SET because I don't know those numbers. What I have now is (the…
Andrei
  • 160
  • 2
  • 14
0
votes
1 answer

FIND_IN_SET or use JOIN?

I have an article table, it can be associated with many categories. Here is solution 1 with FIND_IN_SET: CREATE TABLE `article` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `cat` varchar(512) DEFAULT NULL, `created_at` timestamp NULL…
seaguest
  • 2,510
  • 5
  • 27
  • 45
0
votes
2 answers

How can I avoid duplicate usernames while joining 2 tables

I have two tables related to user information: Table 1(Users) contains columns like id,username and email Table 2(details) contains columns like user_id,degree_name and degree when I am joining these two tables, result I am getting is: name …
0
votes
1 answer

How to get last updated record with FIND_IN_SET in laravel?

I am using find_in_set with my query to get data from comma separated field. But it shows me error. $results = DB::select("SELECT basic.updated_at,basic.updated_by,,ccbi.team_group_id,basic.id FROM basic_info as basic INNER JOIN emp_info as …
amit sutar
  • 541
  • 2
  • 11
  • 37
0
votes
1 answer

How to use wildcard search in FIND_IN_SET() mysql?

I have a stored procedure I want to use Wildcard search in FIND_IN_SET AND IF(ipc IS NOT NULL,FIND_IN_SET(b.ipc_class_symbol, ipc),1=1)); ipc is varchar like "F03D, F02D", etc I want to search in "F03D%, F02D%" which is not giving any results. How…
0
votes
0 answers

MySQL Query FIND_IN_SET on comma separated column to match comma separated ints

I am looking to write an efficient query to match a comma separated list of integers to match on a comma separated column. The tables would look like: Words id word versionId ( comma separated, ex: 1,3 ) I want a where clause where I get all words…
Ice76
  • 1,143
  • 8
  • 16
0
votes
1 answer

mysql check if given string are in a comma separated list without FIND_IN_SET

SELECT * FROM TABLE_NAME WHERE 'string' in ('string1','string2','string3') Its not given correct result find_in_set only given a exact result. If any way to get correct result without using find_in_set
0
votes
0 answers

Find_in_set taking too much time in mysql query

My Query performance is very very bad. It is taking around. SELECT t1.from_state_id fromStateId,GROUP_CONCAT(t3.state_name) fromState FROM tbl_vendor_workstations as t1 Left JOIN tbl_states as t3 ON find_in_set(t3.id,t1.from_state_id) where…
Sucbe Sys
  • 175
  • 1
  • 2
  • 13
0
votes
1 answer

FIND_IN_SET on columns? Does it make sense?

I stumbled across a strange expression, which doesn't make sense to me at all. SELECT * FROM `table` WHERE FIND_IN_SET('string', `column`); Why not: SELECT * FROM `table` WHERE `column`='string'; I thought FIND_IN_SET is used to find strings in…
ulbiopro
  • 3
  • 3
0
votes
2 answers

How can I smart sort my table content and add style inside MySQL query?

table orders: +----+-------+ | id | order | +----+-------+ | 1 | 21 | | 2 | 23 | | 3 | 22 | +----+-------+ table products: +----+--------+-----------+----------+ | id | Name | Category | order_id…
peace_love
  • 6,229
  • 11
  • 69
  • 157
0
votes
2 answers

Find multiple needles in FIND_IN_SET

I have a variable that may contain a comma separated list. In my database, I also have a column with a column separated list. I know I can find a single value in that db column by using FIND_IN_SET(needle, haystack) However, if my variable contains…
Dukatu
  • 65
  • 1
  • 7
0
votes
1 answer

MySQL Get distinct values from custom (|) separated strings

I have a table. Let's name it user_errors user_id errors 1 E001|E003 1 E005|E001|E003 3 E009|E002|E004 2 E001|E005|E004|E006 I have another table which has error description. Let's say…
0
votes
1 answer

Usage of FIND_IN_SET in query

$query=$this->db ->select(a.date,group_concat(s.name) ->from("tbl_attendance a,tbl_students s") ->WHERE ("FIND_IN_SET(s.student_id, a.attendance)")->group_by("a.date") ->get(); I wanted to know whether I have used the FIND_IN_SET and group_by…
julie
  • 111
  • 1
  • 2
  • 13
0
votes
1 answer

Fetch record if found match of one comma separated record with another comma separated records

I have 3 tables user service userServices User Table Id | Name | Status ------------------------ 1 | User1 | y 2 | User2 | y 3 | User3 | y 4 | User4 | y Service Table Id | ServiceName | Status ------------------------ 1 …