3

I am using two tables -institute and category. Institute table is having category id with comma separated values like 1,2. I want to get these comma separated values as IT,Health in MySQL query result.

enter image description here

enter image description here

I am using this below query:

SELECT i.*, c.category_name FROM institute i, category c WHERE FIND_IN_SET(c.category_id, i.institute_category)

Which shows me the below result :

enter image description here

I am looking for comma separated values in categories and dont need duplicate institute id i.e.

enter image description here

deepak bhardwaj
  • 534
  • 3
  • 9
  • 21

2 Answers2

6

For getting your expected results:

This is what your query will look like (Using GROUP BY AND GROUP_CONCAT)

SELECT i.*, GROUP_CONCAT(c.category_name) FROM institute i, category c 
WHERE FIND_IN_SET(c.category_id, i.institute_category) 
GROUP BY i.institute_category
Sumit Pandey
  • 448
  • 2
  • 9
0

Use group_concat function:

SELECT i.*, group_concat(c.category_name) FROM institute i, category c
WHERE FIND_IN_SET(c.category_id, i.institute_category)
Narendrasingh Sisodia
  • 21,247
  • 6
  • 47
  • 54
Ravneet
  • 300
  • 1
  • 5