0

hobby_users Table:

enter image description here

subcategory_master Table:

enter image description here

My Query:

SELECT hobby_users.user_subcategory_id,GROUP_CONCAT(subcategory_master.subcategory_name) AS subcategory_name
        FROM `hobby_users` 
        INNER JOIN subcategory_master
        WHERE hobby_users.hobby_users_id='1' AND subcategory_master.subcategory_master_id IN(SELECT user_subcategory_id FROM `hobby_users` WHERE hobby_users_id='1')

Actual Result:

enter image description here

Expected Result:

enter image description here

Note: The sub-query retuns 1,2

Please suggest or help for where I am wrong.

1
  • If it's not too late, you should seriously reconsider your schema, specifically the user_subcategory_id column. If it is too late, have a look at FIND_IN_SET(). Commented Feb 20, 2017 at 7:35

2 Answers 2

2

For group_concat you should use GROUP BY for grouping

SELECT 
   hobby_users.user_subcategory_id
  ,GROUP_CONCAT(subcategory_master.subcategory_name) AS subcategory_name
FROM `hobby_users` 
INNER JOIN subcategory_master
WHERE hobby_users.hobby_users_id='1' 
  AND subcategory_master.subcategory_master_id IN (SELECT user_subcategory_id FROM `hobby_users` WHERE hobby_users_id='1')
GROUP BY hobby_users.user_subcategory_id
Sign up to request clarification or add additional context in comments.

Comments

0
/*
drop table if exists hobby_users;
create table hobby_users(hobby_users_id int, user_name varchar(3), user_subcategory_id varchar(3));

drop table if exists subcategory_master;
create table subcategory_master(subcategory_master_id int, subcategory_name varchar(3));

insert into hobby_users values
(1,'abc','1,2'),
(2,'def','1,3');

insert into subcategory_master values
(1,'aaa'),(2,'bbb'),(3,'ccc'),(4,'ddd');
*/


MariaDB [sandbox]> SELECT h.hobby_users_id,
    ->    h.user_subcategory_id
    ->   ,GROUP_CONCAT(sm.subcategory_name) AS subcategory_name
    -> FROM `hobby_users` h
    -> INNER JOIN subcategory_master sm
    -> WHERE h.hobby_users_id='1'
    ->   AND
    ->   find_in_set(sm.subcategory_master_id,h.user_subcategory_id) > 0
    -> GROUP BY h.user_subcategory_id;
+----------------+---------------------+------------------+
| hobby_users_id | user_subcategory_id | subcategory_name |
+----------------+---------------------+------------------+
|              1 | 1,2                 | aaa,bbb          |
+----------------+---------------------+------------------+
1 row in set (0.00 sec)

4 Comments

#1054 - Unknown column 'hobby_users.hobby_users_id' in 'where clause'
Substitute the table name with the alias name (h)
It will return subcategory_name as null
Not with the information you provided. I have edited the the solution to include the hobby_users_id test and the result when the code is run.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.