自以为Sql还可以,但写出的结果就是不合适。特此求教:
在Sql语句中,外连接可以查询得出相匹配的数据结果,我想知道不匹配的数据,也就是匹配之后剩下的数据,如何查?

解决方案 »

  1.   

    declare @ta table (id int,va varchar(10))
    declare @tb table (id int,vb varchar(10))
    insert into @ta select 1,'aa' 
    insert into @ta select 2,'bc'
     insert into @ta select 3,'ccc'
     insert into @tb select 1,'2'
     insert into @tb select 3,'58' 
     insert into @tb select 4,'67' 
     --内连接简单写法
     select a.id,a.va,b.id,b.vb from @ta a,@tb bwhere a.id=b.id
     --内连接
     select a.id,a.va,b.id,b.vb 
     from @ta a inner join @tb bon a.id=b.id
     select a.id,a.va,b.id,b.vb 
     from @ta a join @tb bon a.id=b.id
     --左连接(左外连接)
     --返回left join 子句中指定的左表的所有行,以及右表所匹配的行。
     select a.id,a.va,b.id,b.vb from @ta a left join @tb bon a.id=b.id
     select a.id,a.va,b.id,b.vb from @ta a left outer join @tb bon a.id=b.id
     --右连接(右外连接)--返回right join 子句中指定的右表的所有行,以及左表所匹配的行。s
     elect a.id,a.va,b.id,b.vb from @ta a right join @tb bon a.id=b.id
     select a.id,a.va,b.id,b.vb from @ta a right outer join @tb bon a.id=b.id
     --完整外连接--等同左连接+右连接
     select a.id,a.va,b.id,b.vb from @ta a full join @tb bon a.id=b.id
     select a.id,a.va,b.id,b.vb from @ta a full outer join @tb bon a.id=b.id
     --交叉连接--没有两个表之间关系的交叉连接,将产生连接所涉及的表的笛卡尔积。
     select a.id,a.va,b.id,b.vb from @ta a cross join @tb b
     select a.id,a.va,b.id,b.vb from @ta a,@tb b
     --自连接--一个表和其本身连接。
     select a.id,a.va,b.id,b.va from @ta a,@ta b where a.id=b.id+1
      

  2.   

    --如果是两表ID关联的话
    SELECT A.ID,B.ID FROM A
    FULL JOIN B ON A.ID=B.ID
    WHERE A.ID IS NULL AND B.ID IS NULL
      

  3.   

    select 
     a.id,b.id 
    from 
     ta a
    full join 
     tb b  
    on 
     a.id=b.id
    where 
     aid is null and b.id is null
      

  4.   

    参考一下;
    sql多表连接查询inner join, left join , right join ,full join ,cross join 
    Posted on 2008-05-07 21:55 我开始懂了 阅读(7363) 评论(1)  编辑 收藏 网摘 所属分类: DB  inner join,full outer join,left join,right jion
    内部连接 inner join 两表都满足的组合
    full outer 全连 两表相同的组合在一起,A表有,B表没有的数据(显示为null),同样B表有
    A表没有的显示为(null)
    A表 left join  B表 左连,以A表为基础,A表的全部数据,B表有的组合。没有的为null
    A表 right join B表 右连,以B表为基础,B表的全部数据,A表的有的组合。没有的为null
    查询分析器中执行:
    --建表table1,table2:
    create table table1(id int,name varchar(10))
    create table table2(id int,score int)
    insert into table1 select 1,'lee'
    insert into table1 select 2,'zhang'
    insert into table1 select 4,'wang'
    insert into table2 select 1,90
    insert into table2 select 2,100
    insert into table2 select 3,70
    如表
    -------------------------------------------------
    table1|table2|
    -------------------------------------------------
    idname|idscore|
    1lee|190|
    2zhang|2100|
    4wang|370|
    -------------------------------------------------以下均在查询分析器中执行一、外连接
    1.概念:包括左向外联接、右向外联接或完整外部联接2.左连接:left join 或 left outer join
    (1)左向外联接的结果集包括 LEFT OUTER 子句中指定的左表的所有行,而不仅仅是联接列所匹配的行。如果左表的某行在右表中没有匹配行,则在相关联的结果集行中右表的所有选择列表列均为空值(null)。
    (2)sql语句
    select * from table1 left join table2 on table1.id=table2.id
    -------------结果-------------
    idnameidscore
    ------------------------------
    1lee190
    2zhang2100
    4wangNULLNULL
    ------------------------------
    注释:包含table1的所有子句,根据指定条件返回table2相应的字段,不符合的以null显示3.右连接:right join 或 right outer join
    (1)右向外联接是左向外联接的反向联接。将返回右表的所有行。如果右表的某行在左表中没有匹配行,则将为左表返回空值。
    (2)sql语句
    select * from table1 right join table2 on table1.id=table2.id
    -------------结果-------------
    idnameidscore
    ------------------------------
    1lee190
    2zhang2100
    NULLNULL370
    ------------------------------
    注释:包含table2的所有子句,根据指定条件返回table1相应的字段,不符合的以null显示4.完整外部联接:full join 或 full outer join 
    (1)完整外部联接返回左表和右表中的所有行。当某行在另一个表中没有匹配行时,则另一个表的选择列表列包含空值。如果表之间有匹配行,则整个结果集行包含基表的数据值。
    (2)sql语句
    select * from table1 full join table2 on table1.id=table2.id
    -------------结果-------------
    idnameidscore
    ------------------------------
    1lee190
    2zhang2100
    4wangNULLNULL
    NULLNULL370
    ------------------------------
    注释:返回左右连接的和(见上左、右连接)二、内连接
    1.概念:内联接是用比较运算符比较要联接列的值的联接2.内连接:join 或 inner join 3.sql语句
    select * from table1 join table2 on table1.id=table2.id
    -------------结果-------------
    idnameidscore
    ------------------------------
    1lee190
    2zhang2100
    ------------------------------
    注释:只返回符合条件的table1和table2的列4.等价(与下列执行效果相同)
    A:select a.*,b.* from table1 a,table2 b where a.id=b.id
    B:select * from table1 cross join table2 where table1.id=table2.id  (注:cross join后加条件只能用where,不能用on)三、交叉连接(完全)1.概念:没有 WHERE 子句的交叉联接将产生联接所涉及的表的笛卡尔积。第一个表的行数乘以第二个表的行数等于笛卡尔积结果集的大小。(table1和table2交叉连接产生3*3=9条记录)2.交叉连接:cross join (不带条件where...)3.sql语句
    select * from table1 cross join table2
    -------------结果-------------
    idnameidscore
    ------------------------------
    1lee190
    2zhang190
    4wang190
    1lee2100
    2zhang2100
    4wang2100
    1lee370
    2zhang370
    4wang370
    ------------------------------
    注释:返回3*3=9条记录,即笛卡尔积4.等价(与下列执行效果相同)
    A:select * from table1,table2
      

  5.   

    --写错了
    SELECT A.ID,B.ID FROM A
    FULL JOIN B ON A.ID=B.ID
    WHERE A.ID IS NULL OR B.ID IS NULL
    --应该是OR
      

  6.   

    full join 得到的是两表所有的值 不满足条件的空值全部通过条件查询出来就OK,还写错了点
    select 
     a.id,b.id 
    from 
     ta a
    full join 
     tb b  
    on 
     a.id=b.id
    where 
     a.id is null and b.id is null
      

  7.   

    我的这个表中不匹配的值不是空的Null,也有值,也以说IS NULL肯定是不合适的
      

  8.   

    我现在机器上没有SQL,没法测试,小F小三写个建表程序吧。[code=SQL]IF OBJECT_ID('A') IS NOT NULL DROP TABLE A
    IF OBJECT_ID('B') IS NOT NULL DROP TABLE B
    GO
    CREATE TABLE A(ID INT)
    INSERT INTO A SELECT 1 UNION ALL SELECT 2
    CREATE TABLE B(ID INT)
    INSERT INTO B SELECT 3 UNION ALL SELECT 2
    --A和B通过ID关联,关联后匹配的数据为2,不匹配的数据为A的1和B的3,
    --LZ是不是想看到1和3
    SELECT A.ID,B.ID
    FROM A
    FULL JOIN B ON A.ID=B.ID
    WHERE A.ID IS NULL OR B.ID IS NULL--结果集应该是
    /*
    ID ID
    1 NULL
    NULL 3
    */
    code]
      

  9.   

    A表:
    f1  f2
    A   aa
    B   bb
    C   cc
    B表:
    f1  f2  f3
    A   aa  100
    B   bb  200
    C   xx  300SELECT A.F1,A.F2,B.F3 FROM A LEFT JOIN B ON A.F1=B.F1 AND A.F2=B.F2 OR
    SELECT A.F1,A.F2,B.F3 FROM A RIGHT JOIN B ON A.F1=B.F1 AND A.F2=B.F2
    这些结果都是匹配的,我想查询出B表中的C xx 300,如何写?
      

  10.   

    看看我15L写的代码是不是你的意思。通常查找不匹配的记录是通过NOT EXISTS和JOIN WHERE IS NULL实现的,也可以用
    NOT IN和<>ALL()实现,但是比较慢。最好贴出你的测试数据和要求结果,大大们就好写了。
      

  11.   

    SELECT A.*,B.* FROM A
    FULL JOIN B ON A.F1=B.F1 AND A.F2=B.F2
    WHERE A.F1 IS NULL OR B.F1 IS NULL
    试试
      

  12.   

    declare @ta table (id int,va varchar(10))
    declare @tb table (id int,vb varchar(10))
    insert into @ta select 1,'aa' 
    insert into @ta select 2,'bc'
     insert into @ta select 3,'ccc'
     insert into @tb select 1,'2'
     insert into @tb select 3,'58' 
     insert into @tb select 4,'67' 
    --笛卡尔乘积
    select * from @ta,@tb
    /*
    id          va         id          vb
    ----------- ---------- ----------- ----------
    1           aa         1           2
    2           bc         1           2
    3           ccc        1           2
    1           aa         3           58
    2           bc         3           58
    3           ccc        3           58
    1           aa         4           67
    2           bc         4           67
    3           ccc        4           67(9 行受影响)
    */
    --内连接 匹配的
     select a.id,a.va,b.id,b.vb 
     from @ta a 
    inner join @tb b on a.id=b.id
    /*---------- ---------- ----------- ----------
    1           aa         1           2
    3           ccc        3           58(2 行受影响)
    */
    --不匹配的那一部分
     select a.id,a.va,b.id,b.vb 
     from @ta a 
    inner join @tb b on a.id<>b.id
    /*
    id          va         id          vb
    ----------- ---------- ----------- ----------
    2           bc         1           2
    3           ccc        1           2
    1           aa         3           58
    2           bc         3           58
    1           aa         4           67
    2           bc         4           67
    3           ccc        4           67(7 行受影响)
    */
      

  13.   

    参照17L的数据,我想查询那条C xx 300,多谢各位
      

  14.   

    IF OBJECT_ID('A') IS NOT NULL DROP TABLE A
    IF OBJECT_ID('B') IS NOT NULL DROP TABLE B
    GO
    CREATE TABLE A(
    F1 VARCHAR(50)
    ,F2 VARCHAR(50)
    )
    INSERT INTO A
    SELECT 'A',  'aa' UNION ALL 
    SELECT 'B',  'bb' UNION ALL 
    SELECT 'C',  'cc' 
    CREATE TABLE B(
    F1 VARCHAR(50)
    ,F2 VARCHAR(50)
    ,F3 VARCHAR(50)
    )
    INSERT INTO B
    SELECT 'A','aa','100' UNION ALL 
    SELECT 'B','bb','200' UNION ALL 
    SELECT 'C','xx','300'--全部不匹配的数
    SELECT A.*,B.* FROM A 
    FULL JOIN B ON A.F1=B.F1 AND A.F2=B.F2 
    WHERE A.F1 IS NULL OR B.F1 IS NULL --A有B没有的数据
    SELECT A.*,B.* FROM A 
    LEFT JOIN B ON A.F1=B.F1 AND A.F2=B.F2 
    WHERE B.F1 IS NULL--A没有B有的数据
    SELECT A.*,B.* FROM A 
    RIGHT JOIN B ON A.F1=B.F1 AND A.F2=B.F2 
    WHERE A.F1 IS NULL/*
    C cc NULL NULL NULL
    NULL NULL C xx 300C cc NULL NULL NULLNULL NULL C xx 300*/
      

  15.   

    ----------------------------------------------------------------
    -- Author  :fredrickhu(我是小F,向高手学习)
    -- Date    :2009-09-09 12:03:01
    -- Version:
    --      Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86) 
    -- Nov 24 2008 13:01:59 
    -- Copyright (c) 1988-2005 Microsoft Corporation
    -- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
    --
    ----------------------------------------------------------------
    --> 测试数据:[ta]
    if object_id('[ta]') is not null drop table [ta]
    go 
    create table [ta]([id] int,[col2] int)
    insert [ta]
    select 1,4 union all
    select 2,5 union all
    select 3,6
    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go 
    create table [tb]([id] int,[col2] int)
    insert [tb]
    select 2,5 union all
    select 3,6 union all
    select 4,7
    --------------开始查询--------------------------
    select 
     a.id,b.id 
    from 
     ta a
    full join 
     tb b  
    on 
     a.id=b.id
    where 
     a.id is null or b.id is null
    ----------------结果----------------------------
    /* id          id
    ----------- -----------
    1           NULL
    NULL        4(2 行受影响)*/
      

  16.   

    B left A  找null 
      

  17.   

     not exists就不可以了吗?
      

  18.   

    A表: 
    f1  f2 
    A  aa 
    B  bb 
    C  cc 
    B表: 
    f1  f2  f3 
    A  aa  100 
    B  bb  200 
    C  xx  300 SELECT A.F1 FROM A LEFT JOIN B ON A.F1=B.F1 AND A.F2=B.F2
    select * from b where f1 in (select a.f1 from a left join b on a.f1=b.f1 and a.f2=b.f2)
      

  19.   

    虽然结贴了,依然还是要回 /*---------------------------------------
    瘦狼阿亮 | 钱不是问题,问题是没钱!
      -----我乐于享受学习的过程----------------------------------------------Microsoft SQL Server  2000 - 8.00.2039 (Intel X86) 
    May  3 2005 23:18:38 
    Copyright (c) 1988-2003 Microsoft Corporation
    Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 3)---------------------------------------*/--建立测试环境函数表
    declare @ta table (aid smallint,aname varchar(10))
    declare @tb table (bid smallint,bname varchar(10))--录入测试数据
    insert into @ta 
    select 1,'aa' union all
    select 2,'bc' union all 
    select 3,'ccc'
    /*执行结果:
    (所影响的行数为 3 行)
    */
     
    insert into @tb 
    select 1,'2' union all
    select 3,'58'union all
    select 4,'67' 
    /*执行结果:
    (所影响的行数为 3 行)
    */--检查测试数据的录入结果
    select * from @ta
    /*执行结果:
    aid    aname      
    ------ ---------- 
    1      aa
    2      bc
    3      ccc(所影响的行数为 3 行)
    */select * from @tb
    /*执行结果:
    bid    bname      
    ------ ---------- 
    1      2
    3      58
    4      67(所影响的行数为 3 行)
    *//*
    如果楼主要得到不匹配的结果可以使用EXISTS
    得到的效果和执行效率和外连接是一样的
    */
    select * from @ta where not exists (select * from @tb where aid=bid) union all
    select * from @tb where not exists (select * from @ta where aid=bid)
    /*执行结果:
    aid    aname      
    ------ ---------- 
    2      bc
    4      67(所影响的行数为 2 行)
    */