现在在重构一个管理系统,由于原开发人员没有保留源代码与文档,只有数据库的数据与可执行文件。今天碰到的一个需求是:
表结构如下
这是一张存放区域名称的表,只有两个字段,一个是area_code,一个是area_name.表数据结构如下
10   北京市
1001 丰台区
1002 海淀区
.......那么我的问题是,如何由area_code字段,比如这个值--1002 ,但是查询页面能够显示"北京市海淀区"?不能写出代码的话,解决思路也可以(这是最重要的),如果不能单纯由数据库来完成,使用任一动态语言来综合解决也可以。

解决方案 »

  1.   

    --如果只有两级还好点
    if not object_id('tb') is null
    drop table tb
    Go
    Create table tb([area_code] int,[area_name] nvarchar(3))
    Insert tb
    select 10,N'北京市' union all
    select 1001,N'丰台区' union all
    select 1002,N'海淀区'
    Go
    Select a.[area_name]+'-'+b.[area_name]
    from tb a,tb b
    where charindex(ltrim(a.[area_code]),ltrim(b.[area_code]))>0
    and b.[area_code]=1002 and a.[area_code]!=b.[area_code]
    /*
    -------
    北京市-海淀区*/PS:正如一楼所说 这样设计不是很严谨