表格设计
[只有一张桌子]
id列名称
1 id
2 name ={ Just a name of the country,state,city,location}
3 mid ={ id of the upper hierarchy [Ex: if india id is 10 and its state inside that country mid is 10][if state id is 12 and city inside that state mid is 12]}
4 parent tag ={Country,State,City,Location} [which is help to identify what is the column is [country or state or city or location]]]
但是我必须以这种格式在数组中获取值:
[[country,state,city,location][country,state,city,location][country,state,city,location][country,state,city,location][country,state,city,location][country,state,city,location][country,state,city,location][country,state,city,location][country,state,city,location]]
每组数组都应有一个location,该位置所在的城市,该城市所在的州,该州所在的国家
输出数组格式:
a[][] =[[country,state,city,location]
[国家,州,城市,位置] ...等,
答案可以是任何东西 1.查询或子查询:[首选] 2.任何循环语句:
数据库设计
输出格式
答案 0 :(得分:2)
尝试JOIN
SELECT DISTINCT `[` + COT.ParentDescription +','+ ST.ParentDescription +','+ CT.ParentDescription +','+ LT.ParentDescription + `]` AS AddressList
FROM LocationTable LT JOIN CityTable CT ON LT.ParentID = CT.OLMID AND CT.ParnetTag = 'City'
JOIN StateTable ST ON ST.OLMID = CT.ParentID AND ST.ParnetTag = 'State'
JOIN CountryTable COT ON COT.OLMID = ST.ParentID AND ST.ParnetTag = 'Country'
WHERE LT.ParentDescription = 'Gopalapuram' AND LT.IsActive = 1 AND ST.IsActive = 1 AND CT.IsActive = 1 AND COT.IsActive = 1
要获得完整结果,请使用COALESCE
DECLARE @Str VARCHAR(8000)
SELECT @Str = COALESCE(@Str + ' ', '') + AddressList
FROM (
SELECT DISTINCT `[` + COT.ParentDescription +','+ ST.ParentDescription +','+ CT.ParentDescription +','+ LT.ParentDescription + `]` AS AddressList
FROM LocationTable LT JOIN CityTable CT ON LT.ParentID = CT.OLMID AND CT.ParnetTag = 'City'
JOIN StateTable ST ON ST.OLMID = CT.ParentID AND ST.ParnetTag = 'State'
JOIN CountryTable COT ON COT.OLMID = ST.ParentID AND ST.ParnetTag = 'Country'
WHERE LT.IsActive = 1 AND ST.IsActive = 1 AND CT.IsActive = 1 AND COT.IsActive = 1
)X
SELECT @Str
答案 1 :(得分:1)
使用递归CTE可能会生成类似的内容。
一个示例(MS Sql Server 2017,因为还使用了STRING_AGG):
CREATE TABLE countrystatelocations ( Id int primary key, Name nvarchar(42), ParentId int, Tag varchar(8), foreign key (parentId) references countrystatelocations(Id) );
INSERT INTO countrystatelocations (Id, Name, ParentId, Tag) VALUES (11, 'Country1', null, 'COUNTRY'), (12, 'State1', 11, 'state'), (13, 'City1', 12, 'city'), (14, 'Loc1', 13, 'loc'), (15, 'Loc2', 13, 'loc'), (16, 'State2', 11, 'state'), (17, 'City2', 16, 'city'), (18, 'Loc3', 17, 'loc')
WITH RCTE AS ( SELECT country.id AS countryId , state.id AS stateId , city.id AS cityId , 0 AS lvl , city.id AS parentId , city.parentId AS locationId , country.name AS countryName , state.name AS stateName , city.name AS cityName , city.name AS locationName FROM countrystatelocations AS country JOIN countrystatelocations AS state ON state.parentId = country.id AND state.tag = 'state' JOIN countrystatelocations AS city ON city.parentId = state.id AND city.tag = 'city' WHERE country.tag = 'country' UNION ALL SELECT r.countryId , r.stateId , r.cityId , r.lvl + 1 , loc.id , loc.parentId , r.countryName , r.stateName , r.cityName , loc.name FROM RCTE r JOIN countrystatelocations loc ON loc.parentId = r.parentId ) SELECT CONCAT( '[', countryName, ', ', stateName, ', ', cityName, ', [', STRING_AGG(locationName,', '), ']]') AS citylocations FROM RCTE WHERE lvl > 0 GROUP BY countryName, stateName, cityName GO
| citylocations | | :-------------------------------------- | | [Country1, State1, City1, [Loc1, Loc2]] | | [Country1, State2, City2, [Loc3]] |
在 db <>小提琴here
上进行测试