Emerald
Emerald

Reputation: 409

Grouping in mysql query

I have these data inside my database :

enter image description here

And I'm trying to select those data to be displayed in a form with a specific group like this :

enter image description here

At first I am thinking of fetching all data and display with a while loop, but doing that will make the form to display repeated data from column hp_lessons. How do I query it to get the exact data like the expected output in the 2nd picture by using only one query?

Upvotes: 0

Views: 48

Answers (1)

OddBrew
OddBrew

Reputation: 498

You can try with the function GROUP_CONCAT() in MySQL. Here your request would be :

SELECT hp_lessons, GROUP_CONCAT(hp_timeslot SEPARATOR ',') as dates
FROM myTable
GROUP BY hp_lessons

This would give you a column 'dates' in your result, containing a string of all the concerned dates separated by a comma. You would just have to explode it in PHP to have an array. It sure is not the best method, and your database structure is not optimal regarding what you want to achieve, but this will do the work eventually.

Upvotes: 1

Related Questions