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
2
votes
1 answer

Can we use FIND_IN_SET() function for multiple column in same table

NOTE : I tried many SF solution, but none work for me. This is bit challenging for, any help will be appreciated. Below is my SQL-Fiddle link : http://sqlfiddle.com/#!9/6daa20/9 I have tables below: CREATE TABLE `tbl_pay_chat` ( nId int(11) NOT NULL…
sober
  • 23
  • 4
2
votes
1 answer

Using find_in_set() in laravel where clause

I know I can use find_in_set as bellow: select `id` from `questions` where FIND_IN_SET(8, categories); But I am using laravel and I would like to write this query there. I have tried this: $q_category = 8; $object = DB::table("questions") …
M Reza Saberi
  • 7,134
  • 9
  • 47
  • 76
2
votes
2 answers

MySQL - delete where not in find_in_set

I'm trying to find the right syntax to delete records that are not in a comma separated row. table A | id | product_id | attribute_id | |----|------------|--------------| | 1 | 123 | 45 | | 2 | 123 | 46 | | 3 |…
Mitchum
  • 107
  • 2
  • 16
2
votes
3 answers

mysql: finding the shortest and the longest value within an mysql-column

I had a question about finding the shortest und the longest value within an concated string in MySQL-Column "values" . The Problem, the values within the column are concated with "|" and may be diffrent long. Table: ID | values…
Christian Felix
  • 644
  • 1
  • 9
  • 28
2
votes
4 answers

FIND_IN_SET sql query

I have query like below. SELECT Id, FIND_IN_SET('22', Category ) >0 AS `tot_cat` FROM tbl_doctorprofile I am getting result 1 in which row i am getting value '22' else 0.. so result is like below. Id tot_cat ---------- 1 0 2 0 3 1 4 …
2
votes
2 answers

MySQL Select query to fetch record base on list values

I am using MySQL. I have 3 Table as below. Table : subject_Master -------------------------- subjectId | subjectShortName ---------------------------------- 1 | English 2 | French 3 | German 4 | …
fresher
  • 399
  • 4
  • 23
2
votes
3 answers

How to find id in MySQL column array

Company table structure: (`id`=`1`, `name`=`CompanyName`, `members`=`3,52,134,21`) The numbers in the members column are the user id's added to this company. When user with id = 3 OR 52 OR 134 OR 21 is logged in the row above should be returned…
Koen
  • 140
  • 1
  • 10
2
votes
1 answer

codeigniter FIND_IN_SET not working with join

I have a codeigniter-mysql requirement to select values from table_2 which joins table_1 and need to apply where statement in a comma separated field value. Tried as follows, $where = …
Aadi
  • 6,959
  • 28
  • 100
  • 145
2
votes
1 answer

Order by sequence of values in set

How can I have concatenated names ordered by their sequence in the set (it is a variable where I have ids comma separated like this "4,3,2")? So far the names are being ordered by id "2,3,4". This is my subquery. SELECT GROUP_CONCAT(t.name SEPARATOR…
2
votes
2 answers

Mysql GROUP_CONCAT and IN query

I have a table EMPDetails like EmpID EmpName EmpFriendsID 1 Hari 2,3 2 Ramesh 3 Suresh I would like to have a query to retrieve EmpFriends name if i give an EmpID. example if EmpID 1 is provided,result should be 1 Hari 2,3 …
eshaa
  • 386
  • 2
  • 7
  • 26
2
votes
1 answer

REGEX mysql for comma separated values

I have a mysql query in java like public static BusObjectIterator GetEmpObjectsforOrgandMultipleCategory(String ORG, String CATEGORY) { String query=select * from PROJECT_EMP where org = :ORG and category=:CATEGORY; ..... return…
eshaa
  • 386
  • 2
  • 7
  • 26
1
vote
2 answers

FIND_IN_SET - Getting partial output due to doubled values

Table "skus": plant_species_ids 1,2,3,2,3,1 Table "plant_species" id name 1 Plant 1 2 Plant 2 3 Plant 3 The relevant part of my MySQL code: ... GROUP_CONCAT(plant_species.name SEPARATOR ', ') as…
1
vote
0 answers

SELECT query using FIND_IN_SET() to match values in the separated commas

I am trying to do dynamic nav menus for the pages, when admin creates a page, he selects multiple selection for the menus that this page should appears for, I am achieving this through two steps, 1st step adding the selected menus in the database…
wowoffer
  • 13
  • 9
1
vote
2 answers

Comparing lists in MySQL

Say I have a movie table and a genre table. linked through a many to many relationship. TABLE movie id name _ TABLE movie_genre movie_fk genre_fk _ TABLE genre id name So nice and normalised. But I'm also also importing a CSV file into a table,…
Alan
  • 13
  • 3
1
vote
1 answer

Join three tables with comma separated values in mysql

I have 3 tables as below **tbl_link** ╔═════════════╦═══════════════╦═══════════════╗ ║ subject_id ║ system_csv_id ║class_csv_id ║ ╠═════════════╬═══════════════╬═══════════════╣ ║ 1 ║ 4,3 ║ 5,1,3,4 ║ ║ 2 ║ 1,3…
Amer Hamid
  • 145
  • 6
1
2
3
9 10