MySQL query get value comma separated from master detail table -
i've 2 tables in mysql database:
tbl_fruit (fruit_id, fruit_name):
fruit id | fruit_name -------------------------- 1 | apple 2 | banana 3 | mango
tbl_order (order_id, order_name, fruit_id):
order_id | order_name | fruit_id ----------------------------------- 1 | john | 1,2 2 | matt | 1,3 3 | jessica | 1,2,3
expected output :
order_name | selected_fruit ---------------------------------- john | apple, banana matt | apple, mango jessica | apple, banana, mango
i've tried queries did not match expected output. in advance.
solved :
select order_name, (select group_concat(fruit_name separator ', ') tbl_fruits fru find_in_set (fru.fruit_id,ord.fruit_id)) selected_fruit `tbl_order` ord
well, can it, not mean should. can join 2 tables using find_in_set() in subquery , use group_concat() in outer query results back:
select t.order_name, group_concat(t.fruit_name) selected_fruits (select o.order_name, f.fruit_name `order` o inner join fruit f on find_in_set(f.fruit_id, o.fruit_id)) t group t.order_name
Comments
Post a Comment