0

Suppose I have 2 tables obtained from a web form submission, with some checkboxes results stored as "string" format as describerd here in "hobby" column https://dbfiddle.uk/?rdbms=mysql_5.7&fiddle=aff4e50085be40930e223b00a1ac81f3,

How could I join this two tables and obtain the following result?

sub_id datimkt
1 Basket,Gas,SUV
2 Basket,Cabrio,Football,hybrid

I've tried to execute the following query, but it only takes the first result from the checkbox result field.

SELECT GROUP_CONCAT(DISTINCT submission_id)sub_id,
GROUP_CONCAT(DISTINCT c1.option_name)datimkt
FROM form_results d
JOIN field_options c1 ON FIND_IN_SET(c1.option_value, d.datimkt)
GROUP BY d.datimkt;
SELECT*FROM result2;

1 Answer 1

1

You need to group by the submission_id:

like:

SELECT submission_id sub_id,
GROUP_CONCAT(DISTINCT c1.option_name)datimkt
FROM form_results d
JOIN field_options c1 ON FIND_IN_SET(c1.option_value, d.datimkt)
GROUP BY sub_id;
4
  • It doesn't work ... I tried to run the query but the result is still the same. I only get the first value of the string Commented Dec 30, 2020 at 23:26
  • It gives your expected results in the question. When talking about first value in string, are you meaning hobby? because that column is unused in the query. Thanks for the fiddle, however a clearer description and dataset that has a unique match to the expected results will help a lot.
    – danblack
    Commented Dec 30, 2020 at 23:53
  • Yes, the column I mean is "hobby". The main problem is that the database under the web form is very large and there are many tables which could affect the final result. The "hobby" column is a checkbox whose values ​​are concatenated. running the query I get only hobby1 = basketball and not hobby1, hobby2 = basketball, football. Commented Dec 31, 2020 at 0:17
  • Here is another example about the same question Commented Dec 31, 2020 at 0:20

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.