现在有如下三个表a(id,字段A,字段B);
b(id,字段D,字段E);
c(id,字段G,字段H);id 都为自动编号 a.字段A b.字段D均为数字编号 且不存在相等 
表C里面每条记录里的字段G 是a表的字段A 或者b表里面的字段D所填充的
表a 实例如下
----------------
id  字段A  字段B
1    123   hello
2    124   world
3    125   yes
----------------
表b 实例如下
----------------
id  字段D  字段E
1    126   C
2    127   C++
3    128   VB
----------------表c 实例如下
----------------
id  字段G 字段H
1    123   张三
2    126   李四
3    128   赵六
----------------
我要查询C表中的记录假设以c.字段H为条件 如何查询C表中的记录  如果 c.字段G在a表中的字段A集合内 则 查询结果为 c.id,字段G,字段H,a.字段B 如果 c.字段G在b表中的字段D集合内如何查询出 赵六的信息像这样  ----- 3   128   赵六  VB SQL 语句怎么写 试了半天 写不出来啊  也不知道说的清楚不 呵呵  先谢谢了 

解决方案 »

  1.   

    --> 测试数据:#a
    if object_id('tempdb.dbo.#a') is not null drop table #a
    create table #a(id int identity, 字段A int, 字段B varchar(8))
    insert into #a
    select 123, 'hello' union all
    select 124, 'world' union all
    select 125, 'yes'
    --> 测试数据:#b
    if object_id('tempdb.dbo.#b') is not null drop table #b
    create table #b(id int identity, 字段D int, 字段E varchar(8))
    insert into #b
    select 126, 'C' union all
    select 127, 'C++' union all
    select 128, 'VB'
    --> 测试数据:#c
    if object_id('tempdb.dbo.#c') is not null drop table #c
    create table #c(id int identity, 字段G int, 字段H varchar(8))
    insert into #c
    select 123, '张三' union all
    select 126, '李四' union all
    select 128, '赵六'select
    c.*, isnull(a.字段B, b.字段E)i
    from #c c
    left join #b b on c.字段G = b.字段D
    left join #a a on c.字段G = a.字段A
    --where c.字段H =  '赵六'/*
    id          字段G       字段H    i
    ----------- ----------- -------- --------
    1           123         张三     hello
    2           126         李四     C
    3           128         赵六     VB
    */