I have two tables, one for country an the other for the city, and I want a query to get the country with all the cities in a comma-separated which grouped by the countries.
Note:
The second table has a foreign key
Fk_CountryId
from the primary key of the first tableCityId
which is a primary keyI am using SQL Server
The first table Country
looks like this
CountryId | CountryName
----------+-------------
1 | USA
2 | UK
3 | Germany
The second table City
looks like this
CityId | CityName | Fk_CountryId
-------+------------+-------
1 | Los Angeles| 1
2 | Boston | 1
3 | Cambridge | 2
4 | Chester | 2
5 | Berlin | 3
6 | Hamburg | 3
The expected DataSet:
CountryId | CountryName | CitiesNames
----------+--------------+----------------------
1 | USA | Los Angeles, Boston
2 | UK | Cambridge, Chester
3 | Germany | Berlin, Hamburg