0

IN query is not working in sub_query of mysql
Please take look-:

SELECT (SELECT GROUP_CONCAT( text ) 
        FROM course_intersted_in_list 
        WHERE id IN ( urd.interested_course )) as interested_course_text,
       urd.* 
from user_registration_data as urd

Here urd.interested_course have values like 1,2,3,4 for some users

user_registration_data table is like

id             interested_course
 1             1,2,3,4,5
 2             1,4,5

course_intersted_in_list table is like

id             interested_course
 1             mbbs
 2             dental
 3             basic
Vipul sharma
  • 1,245
  • 1
  • 13
  • 33

2 Answers2

2

Since the interested_course has comma separated ids, you can use find_in_set to search a value in csv string:

SELECT (SELECT GROUP_CONCAT( text ) 
        FROM course_intersted_in_list 
        WHERE find_in_set(id, urd.interested_course)
        ) as interested_course_text,
       urd.* 
from user_registration_data urd;

Also, I recommend staying away from CSV data in RDBMS and normalize the data. that way you'll get a system that scales.

Gurwinder Singh
  • 38,557
  • 6
  • 51
  • 76
1

You can use MySQL FIND_IN_SET instead:

SELECT (SELECT GROUP_CONCAT( text ) 
        FROM course_intersted_in_list 
        WHERE find_in_set(id, urd.interested_course)) as interested_course_text,
       urd.* 
from user_registration_data as urd

IN() doesn't work on table columns, only against a list inside ()

sagi
  • 40,026
  • 6
  • 59
  • 84