--有如下三张表
create table tablea
(
  type int default 0,
  value nvarchar(10)
)
create table tableb
(
  id int, -- 主键
  le int default 0,
)
create table tablec
(
  id int, -- 主键
  bid int, -- 对应tableb中的id
  value nvarchar(10)
)数据
tablea              tableb            tablec
type   value        id     le         id    bid    value
0      abc          1      1          1     1      abc
-1     asdf         2      1          2     1      def
0      345          3      2          3     1      aaa
-1     dddd                           4     2      abc
                                      5     2      asdf
                                      6     2      345
                                      7     2      fff
                                      8     3      sdf
                                      9     3      dddd
--要求查出来的数据为 (说明,如果tablec.value在tablea.value中找到,则type为tablea.type,否则为tableb.le)
bablec.id       tableb.bid       type     value
1               1                 0        abc
2               1                1        def
3               1                1        aaa
4               2                0        abc
5               2                -1       asdf
6               2                0        345
7               2                1        fff    
8               3                2        sdf
9               3                -1       ddd 
不知大家能看懂不

解决方案 »

  1.   

    select tablec.id,tablec.bid,
    isnull(tablea.type,tableb.le) as type,tablec.value
    from tablec left join tablea on tablec.value=tablea.value
    left join tableb on tablec.bid=tableb.id
      

  2.   

    select
     c.* isnull(a.type,b.le) as type,
    from
     tablec c 
    left join 
     tablea a 
    on 
     c.value=a.value
    left join 
     tableb b
    on 
     c.bid=b.id
      

  3.   

    select
     c.*,isnull(a.type,b.le) as type,
    from
     tablec c 
    left join 
     tablea a 
    on 
     c.value=a.value
    left join 
     tableb b
    on 
     c.bid=b.id
      

  4.   

    你好,这样可以查询出数据,但是这样查询出来的数据比 tablec表中的数据要多,而我要求查询出来的数据记录条数还是tablec中的记录条数
      

  5.   

    贴出你基于下面数据SQL语句的结果
      

  6.   

    这位大大准备混MSSQL板块了?
      

  7.   

    刚刚我查询出来以后,好像数据比tablec中的数据多了,我再测一下..现在在搞一个外包系统,要处理的数据比较复杂,而对SQL又不太熟,根据自己的想像想把SQL简化一下,以达到更优化的查询结果,所以来这里请各位大大们指点
      

  8.   

    你提供的测试数据中tablea              tableb均没有重复数据,要注意。举例也是一种学问。能靠一组测试数据来判断别人提供的方案是否正确不是件容易的事。
      

  9.   

    isnull(a.type,b.le) as type这条语句不太明白,为什么当a.type存在的时候值为a.type,而不存在时却为b.le
      

  10.   

    请问一下,如果tablea,或tableb中存在重复数据,那该怎么办
      

  11.   

    这个ISNULL函数你可以直接在SQL SERVER的在线帮助手册中看到它的用法。这种情况下,你自己首先要知道你想要的结果是什么。
     (不要高估你的汉语表达能力或者我的汉语理解能力)
       建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
       参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
       
       1. 你的 create table xxx .. 语句
       2. 你的 insert into xxx ... 语句
       3. 结果是什么样,(并给以简单的算法描述)
       4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
       
       这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。   
      

  12.   

    先结贴,至于tablea和tableb中的重复数据可以控制,如果没有重复数据这个答案正是我要的.
      

  13.   


    这位任兄,不好意思,这并不是作业,呵呵,因为我对SQL并不太熟,我是搞C的.呵呵...