简体   繁体   中英

How to get name of comma separated id in mysql?

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.

在此处输入图片说明

在此处输入图片说明

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 :

在此处输入图片说明

I am looking for comma separated values in categories and dont need duplicate institute id ie

在此处输入图片说明

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM