I have these tables :
TBL_PERSONS – a table for persons
TBL_SPORTS – a table for sports like football , basketball , tennis and …
TBL_PERSON_SPORT – a table for any person’s sports
for example Jo can play Football , and Basketball
I want to create a VIEW on persons and their sports.
something like this:
Jo|Football,Basketball
Jack|Football,Tennis
how should I write such VIEW?
thanks.
If you really want them to be a comma-separated list, that is the function of the
GROUP_CONCAT()aggregate function. Substitute the correct names of your columns in place ofname,person_id,sport_id, etc…The above won’t be very useful for joining against though, if you need to be able to separate out sports from the comma-separated list. Consider also just wrapping the ungrouped list in a view as:
This will produce one row per sport, containing the person’s name and sport name, where the person is duplicated as many times as needed for each sport.