Let's say I have two tables named Person and Visits. Person lets say Ram could have visited many places like Delhi, Paris, etc. Now how could I write a query in a way that I get Ram and all places he has visited in a list format in SQL rather than in as different rows.
Asked
Active
Viewed 41 times
0
-
1In that case you want `GROUP_CONCAT()`: [See this question for a how-to](https://stackoverflow.com/questions/149772/how-to-use-group-by-to-concatenate-strings-in-mysql) – JNevill Jul 17 '19 at 14:27
1 Answers
0
Suppose you have primary key id
in table Person
and foreign key person_id
in table Visits
so you can JOIN
this tables,
and to get places like list you have to use function GROUP_CONCAT
.
Your final query must looks like this:
SELECT p.name, GROUP_CONCAT(v.place) AS VisitedPlaces
FROM Person AS p
INNER JOIN Visits AS v ON p.id = v.person_id
WHERE p.name = 'Ram'
;