表一
id 名称 型号
1   1  1
4   4    4
表二
id 质量 尺寸
1   1    1
2   2    2
3   3    3
表三
id 名称 型号 质量 尺寸
1   1   1    1   1
2            2   2
3            3   3
4   4   4
请问用什么语句检索得到第三个表

解决方案 »

  1.   

    left join 会漏掉表二的2、3记录而且还不止表1、2两个有好几个记录相互交叉的表,
    想取个最大集合,用union又不好合并相同id的
      

  2.   

    表一 
    id   名称   型号 
    1       1   1 
    4       4         4 
    表二 
    id   质量   尺寸 
    1       1         1 
    2       2         2 
    3       3         3 
    表三 
    id   质量2   尺寸2 
    5       5         5 
    6       6         6 
    4       42        42 
    表三 
    id   名称   型号   质量   尺寸    质量2   尺寸2 
    1       1       1         1       1 
    2                         2       2 
    3                         3       3 
    4       4       4                          42          42
    5                                           5           5
    6                                           6           6
      

  3.   

    select a.*,b.质量,b.尺寸   From 表一 a ,表二  b where a.id=b.id
      

  4.   

    select a.*,isnull(b.质量,'') 质量,isnull(b.尺寸,'') 尺寸   From 表一 a left join 表二  b on a.id=b.id
      

  5.   

    select a.*,isnull(b.质量,'') 质量,isnull(b.尺寸,'') 尺寸   From 表一 a left join 表二  b on a.id=b.id
    union 
    select id, 质量 as  名称, 尺寸 as 型号,'','' from 表二 where id not in (select id from 表一)
      

  6.   

    create table 表一 (id int ,名称 varchar(10),型号 varchar(10) ) 
    go
    insert 表一 select 1, '1', '1' 
    insert 表一 select 4, '4', '4' 
    create table 表二 (id int, 质量 varchar(10), 尺寸 varchar(10) )
    go
    insert 表二 select 1, '1', '1' 
    insert 表二 select 2, '2', '2' 
    insert 表二 select 3 ,'3', '3' 
    go
    select a.*,isnull(b.质量,'') 质量,isnull(b.尺寸,'') 尺寸   From 表一 a left join 表二  b on a.id=b.id
    union 
    select id, 质量 as  名称, 尺寸 as 型号,'','' from 表二 where id not in (select id from 表一)drop table 表一,表二
    go
    /*
    id          名称         型号         质量         尺寸         
    ----------- ---------- ---------- ---------- ---------- 
    1           1          1          1          1
    2           2          2                     
    3           3          3                     
    4           4          4                     (所影响的行数为 4 行)*/
      

  7.   

    create table tb1(id int,名称 varchar(20),型号 varchar(20))
    insert into tb1 values (1,'1','1')
    insert into tb1 values (4,'4','4')create table tb2(id int,质量 varchar(20),尺寸 varchar(20))
    insert into tb2 values (1,'1','1')
    insert into tb2 values (2,'2','2')
    insert into tb2 values (3,'3','3')
    create table tb3(id int,质量2 varchar(20),尺寸2 varchar(20))
    insert into tb3 values (5,'1','5')
    insert into tb3 values (6,'6','6')
    insert into tb3 values (4,'42','42')
    select tb2.id,名称,型号,isnull(tb2.质量,'') 质量,isnull(tb2.尺寸,'') 尺寸,'' as 质量2,''尺寸2  from tb2  left join tb1  on tb1.id=tb2.id
    union 
    select tb3.id,名称,型号,'','',isnull(tb3.质量2,'') 质量2,isnull(tb3.尺寸2,'') 尺寸2   from tb3  left join tb1  on tb1.id=tb3.id
      

  8.   

    没看清问题,给你第三个表
    create table 表一 (id int ,名称 varchar(10),型号 varchar(10) ) 
    go
    insert 表一 select 1, '1', '1' 
    insert 表一 select 4, '4', '4' 
    create table 表二 (id int, 质量 varchar(10), 尺寸 varchar(10) )
    go
    insert 表二 select 1, '1', '1' 
    insert 表二 select 2, '2', '2' 
    insert 表二 select 3 ,'3', '3' 
    go
    select a.*,isnull(b.质量,'') 质量,isnull(b.尺寸,'') 尺寸   From 表一 a left join 表二  b on a.id=b.id
    union 
    select id, '','',质量 , 尺寸 from 表二 where id not in (select id from 表一)drop table 表一,表二
    go
    /*
    id          名称         型号         质量         尺寸         
    ----------- ---------- ---------- ---------- ---------- 
    1           1          1          1          1
    2                                 2          2
    3                                 3          3
    4           4          4                     (所影响的行数为 4 行)
    */
      

  9.   

    select tb2.id,isnull(名称,''),isnull(型号,''),isnull(tb2.质量,'') 质量,isnull(tb2.尺寸,'') 尺寸,'' as 质量2,''尺寸2  from tb2  left join tb1  on tb1.id=tb2.id
    union 
    select tb3.id,isnull(名称,''),isnull(型号,''),'','',isnull(tb3.质量2,'') 质量2,isnull(tb3.尺寸2,'') 尺寸2   from tb3  left join tb1  on tb1.id=tb3.id
      

  10.   

    create table 表一 (id int ,名称 varchar(10),型号 varchar(10) ) 
    go
    insert 表一 select 1, '1', '1' 
    insert 表一 select 4, '4', '4' 
    create table 表二 (id int, 质量 varchar(10), 尺寸 varchar(10) )
    go
    insert 表二 select 1, '1', '1' 
    insert 表二 select 2, '2', '2' 
    insert 表二 select 3 ,'3', '3' 
    go
    create table 表三(id int,  质量2 varchar(10),  尺寸2  varchar(10)) 
    go
    insert 表三 select 5,      '5' ,       '5' 
    insert 表三 select 6,      '6'  ,      '6' 
    insert 表三 select 4,      '42',        '42'
    go
    select a.*,isnull(b.质量,'') 质量,isnull(b.尺寸,'') 尺寸,'' 质量2,'' 尺寸2   From 表一 a left join 表二  b on a.id=b.id
    union 
    select id, '','',质量 , 尺寸,'' 质量2,'' 尺寸2 from 表二 where id not in (select id from 表一)
    union 
    select id ,'','','','',质量2,尺寸2 from 表三
    drop table 表一,表二,表三
    go
    /*
    id          名称         型号         质量         尺寸         质量2        尺寸2        
    ----------- ---------- ---------- ---------- ---------- ---------- ---------- 
    1           1          1          1          1                     
    2                                 2          2                     
    3                                 3          3                     
    4                                                       42         42
    4           4          4                                           
    5                                                       5          5
    6                                                       6          6
    */
      

  11.   

    create table 表一 (id int ,名称 varchar(10),型号 varchar(10) ) 
    go
    insert 表一 select 1, '1', '1' 
    insert 表一 select 4, '4', '4' 
    create table 表二 (id int, 质量 varchar(10), 尺寸 varchar(10) )
    go
    insert 表二 select 1, '1', '1' 
    insert 表二 select 2, '2', '2' 
    insert 表二 select 3 ,'3', '3' 
    go
    create table 表三(id int,  质量2 varchar(10),  尺寸2  varchar(10)) 
    go
    insert 表三 select 5,      '5' ,       '5' 
    insert 表三 select 6,      '6'  ,      '6' 
    insert 表三 select 4,      '42',        '42'
    go
    select a.*,isnull(b.质量,'') 质量,isnull(b.尺寸,'') 尺寸,isnull(c.质量2,'') 质量2,isnull(c.尺寸2,'') 尺寸2  From 表一 a left join 表二  b  on a.id=b.id left join 表三 c on a.id=c.id
    union 
    select id, '','',质量 , 尺寸,'' 质量2,'' 尺寸2 from 表二 where id not in (select id from 表一)
    union 
    select id ,'','','','',质量2,尺寸2 from 表三 where id not  in (select id from 表一)
    drop table 表一,表二,表三
    go
    /*
    id          名称         型号         质量         尺寸         质量2        尺寸2        
    ----------- ---------- ---------- ---------- ---------- ---------- ---------- 
    1           1          1          1          1                     
    2                                 2          2                     
    3                                 3          3                     
    4           4          4                                42         42
    5                                                       5          5
    6                                                       6          6
    */
      

  12.   

    表1
    id 名称 型号
    1 名称1 名称1
    4 名称4 名称4表2
    id 质量 尺寸
    1 质量1 质量1
    2 质量2 质量2
    3 质量3 质量3表3
    id 数量 体积
    2 数量2 体积2
    4 数量4 体积4表4
    id 库存
    2 库存2
    5 库存5最终检索结果
    id 名称 型号 质量 尺寸 数量 体积 库存
    1 名称1 型号1 质量1 尺寸1         
    2       质量2 尺寸2 数量2 体积2 库存2
    3       质量3 尺寸3         
    4 名称4 型号4       数量4 体积4   
    5                   库存5
      

  13.   

    create table 表一 (id int ,名称 varchar(10),型号 varchar(10) ) 
    go
    insert 表一 select 1, '1', '1' 
    insert 表一 select 4, '4', '4' 
    create table 表二 (id int, 质量 varchar(10), 尺寸 varchar(10) )
    go
    insert 表二 select 1, '1', '1' 
    insert 表二 select 2, '2', '2' 
    insert 表二 select 3 ,'3', '3' 
    go
    create table 表三(id int,  质量2 varchar(10),  尺寸2  varchar(10)) 
    go
    insert 表三 select 5,      '5' ,       '5' 
    insert 表三 select 6,      '6'  ,      '6' 
    insert 表三 select 3,      '42',        '42'
    go
    select a.*,isnull(b.质量,'') 质量,isnull(b.尺寸,'') 尺寸,isnull(c.质量2,'') 质量2,isnull(c.尺寸2,'') 尺寸2  From 表一 a left join 表二  b  on a.id=b.id left join 表三 c on a.id=c.id
    union 
    select id, '','',质量 , 尺寸,'' 质量2,'' 尺寸2 from 表二 where id not in (select id from 表一)
    union 
    select id ,'','','','',质量2,尺寸2 from 表三 where id not  in (select id from 表一)
    drop table 表一,表二,表三
    go
    /*id          名称         型号         质量         尺寸         质量2        尺寸2
    ----------- ---------- ---------- ---------- ---------- ---------- ----------
    1           1          1          1          1                     
    2                                 2          2                     
    3                                                       42         42
    3                                 3          3                     
    4           4          4                                           
    5                                                       5          5
    6                                                       6          6
    */
      

  14.   

    if object_id('[ta]') is not null drop table [ta]
    go
    create table [ta]([id] int,[名称] int,[型号] int)
    insert [ta]
    select 1,1,1 union all
    select 4,4,4
    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([id] int,[质量] int,[尺寸] int)
    insert [tb]
    select 1,1,1 union all
    select 2,2,2 union all
    select 3,3,3--select * from [ta]
    --select * from [tb]select id=isnull(a.id,b.id),a.[名称],a.[型号],b.[质量],b.[尺寸] 
    from ta a full join tb b 
    on a.id=b.id
    order by id
    --测试结果:
    /*
    id          名称          型号          质量          尺寸
    ----------- ----------- ----------- ----------- -----------
    1           1           1           1           1
    2           NULL        NULL        2           2
    3           NULL        NULL        3           3
    4           4           4           NULL        NULL(4 行受影响)
    */
      

  15.   


    create table 表一 (id int ,名称 varchar(10),型号 varchar(10) ) 
    go
    insert 表一 select 1, '1', '1' 
    insert 表一 select 4, '4', '4' 
    create table 表二 (id int, 质量 varchar(10), 尺寸 varchar(10) )
    go
    insert 表二 select 1, '1', '1' 
    insert 表二 select 2, '2', '2' 
    insert 表二 select 3 ,'3', '3' 
    go
    create table 表三(id int,  质量2 varchar(10),  尺寸2  varchar(10)) 
    go
    insert 表三 select 5,      '5' ,       '5' 
    insert 表三 select 6,      '6'  ,      '6' 
    insert 表三 select 3,      '42',        '42'
    go
    select a.*,isnull(b.质量,'') 质量,isnull(b.尺寸,'') 尺寸,isnull(c.质量2,'') 质量2,isnull(c.尺寸2,'') 尺寸2  From 表一 a left join 表二  b  on a.id=b.id left join 表三 c on a.id=c.id
    union 
    select id, '','',质量 , 尺寸,'' 质量2,'' 尺寸2  from 表二 where id not in (select id from 表一)union 
    select id ,'','','','',质量2,尺寸2 from 表三 
    where id not  in (select id from 表二) and 
    id not in(select id from 表一)          --改了下
    drop table 表一,表二,表三
    go
    /*id          名称         型号         质量         尺寸         质量2        尺寸2
    ----------- ---------- ---------- ---------- ---------- ---------- ----------
    1           1          1          1          1                     
    2                                 2          2                     
    3                                 3          3                     
    4           4          4                                           
    5                                                       5          5
    6                                                       6          6
    */改了下,就好了
      

  16.   


    full join 可以这样就好了!
      

  17.   

    full join SQL2000 好像没有
      

  18.   

    full join SQL2000 好像没有
      

  19.   

    有。
    写完整点是: full outer join
    外联的几种方式之一。
      

  20.   

    谢谢
    Access有没有这个full join 
      

  21.   

    没有。
    但是可以用变通的方法:先left join,再right join,然后把结果集合并。
    SELECT table1.*, table2.*
    FROM table1 LEFT JOIN table2
    ON table1.id = table2.id
    UNION --ALL
    SELECT table1.*, table2.*
    FROM table1 RIGHT JOIN table2
    ON table1.id = table2.id
      

  22.   

    我那8个表要怎么先left join,再right join才可以:(组合次数太多了:(
      

  23.   

    先把所有id放入一个临时表中,再用这个临时表依次left join。
    select distinct id into # from
    (select id from t1  
    union all select id from t2 
    union all select id from t3 
    --union all...
    ) tselect #.id,t1.col1,t2.col2 --,...
    from # 
    left join t1 on #.id=t1.id
    left join t2 on #.id=t2.id
    --...