SQL Server level three linkage, recursive table

Source: Internet
Author: User

SQL Server provincial and county level three linkage

Three tables recursively merge into a table SQL as follows:

Insert into table2 (area_name,area_parent_id)
Select Province, ' 0 ' from provinces
Insert into table2 (area_name,area_parent_id)
Select City,c.id from Cities a
Inner JOIN provinces B on A.provinceid=b.provinceid
INNER JOIN table2 C on B.province=c.area_name
Insert into table2 (area_name,area_parent_id)
Select A.area,c.id from Areas a
Inner JOIN cities B on A.cityid=b.cityid
INNER JOIN table2 C on B.city=c.area_name and area_parent_id<>0

SELECT * FROM Table2

Here are three table data:

http://files.cnblogs.com/files/sunqiang/%E5%85%A8%E5%9B%BD%E7%9C%81%E5%B8%82%E5%8C%BA%E8%81%94%E5%8A%A8%E6%95% B0%e6%8d%ae%e5%ba%93sql%e6%a0%bc%e5%bc%8f%28mysql%29.rar

SQL Server level three linkage, recursive table

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.