数据库表里存储的数据是全世界的国家以及各个国家所属的市区数据,
求帮忙指导下如何只筛选出中国相关的地区和省市数据
表如下图
------解决思路----------------------
;WITH c AS (
SELECT id,NAME,parentid,TYPE,postcode FROM tb WHERE id=1
UNION ALL
SELECT t.id,t.NAME,t.parentid,t.type,t.postcode FROM tb t INNER JOIN c ON c.id=t.parentid
)
SELECT * FROM c
------解决思路----------------------
SELECT t1.Name 国,
t2.Name 省,
t3.Name 市,
t4.Name 区
FROM table1 t1
LEFT JOIN table1 t2
ON t1.Id = t2.ParentId
LEFT JOIN table1 t3
ON t2.Id = t3.ParentId
LEFT JOIN table1 t4
ON t3.Id = t4.ParentId
WHERE t1.Id = 1
固定层数的更简单了