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
63
votes
2 answers

Join two tables with comma separated values

I have 2 tables as below Notes Table ╔══════════╦═════════════════╗ ║ nid ║ forDepts ║ ╠══════════╬═════════════════╣ ║ 1 ║ 1,2,4 ║ ║ 2 ║ 4,5 ║ ╚══════════╩═════════════════╝ Positions…
626
  • 1,159
  • 2
  • 16
  • 27
26
votes
2 answers

Select Multiple Ids from a table

I want to select some id's based on url string but with my code it displays only the first. If i write manual the id's it works great. I have a url like this http://www.mydomain.com/myfile.php?theurl=1,2,3,4,5 (ids) Now in the myfile.php i have my…
Irene T.
  • 1,393
  • 2
  • 20
  • 40
15
votes
2 answers

Codeginter $this->db->where() function concating "IS NULL" in query automatically when using find_in_set() function

I am writing a query in codeigniter with FIND_IN_SET() function. $this->db->where(FIND_IN_SET('".$value."',employer_job_location)); $query_res= $this->db->get("employer_posted_jobs"); echo $this->db->last_query(); exit; It is yielding…
Aabshar Pasha
  • 194
  • 2
  • 10
5
votes
3 answers

mysql FIND_IN_SET equivalent to postgresql

select * from folder f,uploads u where u.id=f.folderId and FIND_IN_SET('8', '15,9,13,27') Please tell to me equivalent to predefind or userdefined postgresql function
selva
  • 91
  • 1
  • 2
  • 7
5
votes
4 answers

MySQL: Unable to select the records from specific partitions?

I am working with MySQL 5.6. I had created a table with 366 partitions to save data daywise means In a year we have maximum 366 days so I had created 366 partitions on that table. The hash partitions were managed by an integer column which stores 1…
Saharsh Shah
  • 28,687
  • 8
  • 48
  • 83
4
votes
1 answer

Speed of query using FIND_IN_SET on MySql

i have several problems with my query from a catalogue of products. The query is as follows: SELECT DISTINCT (cc_id) FROM cms_catalogo JOIN cms_catalogo_lingua ON ccl_id_prod=cc_id JOIN cms_catalogo_famiglia ON (FIND_IN_SET(ccf_id, cc_famiglia) !=…
Lorenzo Belfanti
  • 1,205
  • 3
  • 25
  • 51
3
votes
1 answer

MYSQL query not working with Greek words in where or FIND_IN_SET

There are two records with ABITE and ABİDE name in the database but below query always return ABITE row in the query response because of Greek character in ABİDE. SELECT * FROM `namedetails` WHERE FIND_IN_SET (CitizenName,'ABITE,ABİDE')>0 May I…
Dilip Kheni
  • 485
  • 1
  • 3
  • 17
3
votes
3 answers

How to use FIND_IN_SET in jpa

How can I use find_in_set in jpa? I need to achieve like this SQL - select * from teacher where find_in_set("5", deptIds) and id = 101 where deptIds have comma separated ids (I know it's bad idea but legacy.) To do so I had been tried using…
The Hunter
  • 155
  • 2
  • 8
3
votes
1 answer

MYSQL - get value from position of comma-separated-string

I have a field in my table like this: fox,cat,bear,horse,dog With FIND_IN_SET I can find if the value is in that string and get back the position of it. There is a way to get value of a determinate position? for example: position 3 = bear position 2…
user2940867
3
votes
2 answers

comma separated argument for IN operator, MySQL

I have a table in which the following query works fine: select * from session_actions where action_type IN ('login_failed','channel_recorded') Now I'm looking forward to some thing like the following: select * from session_actions where…
Mehdi Karamnejad
  • 144
  • 2
  • 11
3
votes
1 answer

Using DISTINCT with FIND_IN_SET

I want to select DISTINCT(p.ptype) at the same time I also want to get the c.category if p.ptype is not in the set of c.ptype Database Table: p id ptype 1 Shirts 2 Cups 3 Shirts 4 Mugs Database Table: c id category ptype 1 Test …
Saad Bashir
  • 4,341
  • 8
  • 30
  • 60
3
votes
1 answer

IN operator not working with string values

I am using the following query to filter records from my table: SELECT * FROM teacher_info WHERE teaching_locations in ('Salt Lake City'); It's returning an empty result even though I have the following values stored in the table in the…
user2623213
  • 233
  • 2
  • 4
  • 10
3
votes
2 answers

GROUP_CONCAT with FIND_IN_SET, multiple joins

I want to retrieve items which have certain filters set. For example list items which are red or blue and small should return only the item apple. ((red(2) or blue(4)) and small(5)) => apple I have found 2 solutions, but both seem to me overly…
user5542121
  • 1,051
  • 12
  • 28
3
votes
5 answers

MySQL - WHERE IN with a list into a field

I have a field ('roles') with this values roles row_1: 1,2,3,5 row_2: 2,13 I do this: SELECT * FROM bloques WHERE 2 IN (roles) and only find row_2, because it starts by 2 The LIKE option doesn't work because if I find 1 SELECT * FROM…
2
votes
1 answer

Find in set to get the separate values with comma

Before I have asked the same problem (Join table with comma issue (MySQL)) about join table with comma in the column. I have two tables, table structure like below: First Table name: student id | name | course_id —————————————————————————— 1 …
David Holly
  • 373
  • 1
  • 9
1
2 3
9 10