您的位置:程序门 -> ms-sql server -> 应用实例



多表查询的问题


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


多表查询的问题
发表于:2007-08-10 18:26:57 楼主
多表查询
一共五个表,其中四个表(a,b,c,d)和另外一个表(f)关联
现要用一个查询语句把表(f)中的某三个字段查出来(这三个必须查出来)、另外四个表中也有三个字段(这三个字段可能为空)需要查出来,这个语句要怎么写?
发表于:2007-08-10 18:34:101楼 得分:0
select   f.3个字段,a.3个字段,b.3个字段,c.3个字段,d.3个字段
from   f  
left   join   a   on   f.x=a.x
left   join   b   on   f.x=b.x
left   join   c   on   f.x=c.x
left   join   d   on   f.x=d.x
where   .............

看你的要求,用左联,别的我就看不出了


发表于:2007-08-10 18:39:102楼 得分:0
select   f.col1,f.col2,f.col3,a.col4,b.col5,c.col6  
from   f  
left   join   a   on   f.id   =   a.id  
left   join   b   on   f.id2   =   b.id
left   join   c   on   f.id3   =   c.id
left   join   d   on   f.id4   =   d.id
发表于:2007-08-10 18:56:473楼 得分:0
select     distinct
a.employeecode   ,   a.employeename,   ,     a.retirement_day     b.belong2name   ,  
c.belong3name,   d.storename
from    
a   ,b   ,c     ,d   ,   f  
where          
        a.employeemanagementid   =   f.employeemanagementid
and   c.belong3code=f.division
and   b.belong2code=f.belonging
and   d.storecode=f.store
and   a.retirement_day= '2007-04-30   00:00:00.000 '


麻烦帮我看下这个该怎么改阿?   其中:a.employeecode   ,   a.employeename,   ,     a.retirement_day   这三个必须查出来,其他三个b.belong2name   ,   c.belong3name,   d.storename
可能为空,现在这个语句出不出空的,要所有项都有数据的才能查出来,急啊
帮我看下怎莫改?
发表于:2007-08-10 21:37:364楼 得分:0
----抱歉,开始那帖看错了
try:
---------------------------------------------------------------------
select   distinct
              a.employeecode,a.employeename,a.retirement_day,b.belong2name,  
              c.belong3name,d.storename
from    
        f
left   join   a
          on   a.employeemanagementid   =   f.employeemanagementid
left   join   c  
          on   c.belong3code=f.division
left   join   b  
          on   b.belong2code=f.belonging
left   join   d  
          on   d.storecode=f.store
where   a.retirement_day= '2007-04-30   00:00:00.000 '
发表于:2007-08-10 21:38:305楼 得分:0
ljsql(第   1   行:   '脑子 '   附近有语法错误。)  
-----------------------------------------
升星了,恭喜恭喜!!!
发表于:2007-08-11 23:05:176楼 得分:0
mark


快速检索

最新资讯
热门点击