Doing two SELECT query, with the second query dependant of the first, and the result of the second being concatenated in one
So I have those two tables:
+----------+ +---------------+
|tGathering| |tGatheredPeople|
+----------+ +---------------+
|cId | |cGatheringID |
|cPlace | |cPeople |
|... | +---------------+
+----------+
I want in one select query to retrieve data about Gatherings in one Place (say "Berlin, Germany"), and amongst the 开发者_如何学Govalues, would be a comma-separated string of all the people present at one gathering.
Is it possible to do it in one query? And if yes, how does one call this kind of query (as you may have noticed, I had difficulties defining what I was searching for)?
Check out the GROUP_CONCAT function
An example usage would be:
SELECT tGathering.*, GROUP_CONCAT(people.cPeople SEPARATOR ',') FROM tGathering
INNER JOIN tGatheredPeople AS people ON `people`.cGatheringID=`tGathering`.cId
GROUP BY people.cGatheringID
Yes, it is possible to do in one query, as long as you prepare the list of PeopleByPlaceId in a function that you would apply to each place Id. Something like:
Select t.*, PeopleByPlaceId(t.CId) as ListOfPeople
from TGathering t
In the function you would build the concatenated list of people names.
How to do this without using a function.. I don't know :-).
精彩评论