/* * You could use `GROUP_CONCAT()` to concatenate all subtopics into one * string per topic, and then parse the string in your application code. */ SELECT topic_name, GROUP_CONCAT(subtopic_name DELIMITER '§§§') as subtopic_names FROM questions2 GROUP BY topic_name /* * But i do not recommend that, because you will get in troubles, if a * subtopic contains your delimiter. I would just use your second query * and group the result in the application code. * * PHP code would look something like: * * <!-- language: lang-php --> */ // group the data $groupedData = array(); foreach ($data as $item) { $topic_name = $item['topic_name']; $subtopic_name = ucwords($item['subtopic_name']