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.