不知道这样问对不对,有2张表
    t1                t2                     t1 + t2 
a  b  c  d      a1  b1  c1  d1        a2  b2  c2  d2  e2    f2
5  1  0  3      N   1   N   x         5   1   0   3   1     x    --只中1个,中 1
2  0  4  5 -->  2   N   N   x  --->   2   0   4   5   2     y    --只中1个,中 2
4  0  3  2      N   N   3   x         4   6   3   2   4/3   y/x  --中了2个,中 3 中 4
2  1  8  7      4   N   N   y         2   1   8   7   2/1   x/x  --中了2个,中 2 中 1
5  4  7  7                            5   4   7   7              --一个都不中t1的a\b\c三个字段分别和t2的a1\b1\c1比较
1 如果有相同,则在形成的视图中显示t1的所有字段,视图e2,f2则显示t2的相同字段值和标注值(x/y)
2 如果没有字段值相同,则一样把t1相关字段值放入视图,但e2\f2为空这个该如何实现呢? 好头大
谢谢各位大大的指点 ^_^

解决方案 »

  1.   

        t1                 t2                        t1 + t2 
    a  b  c  d       a1  b1  c1  d1        a2  b2  c2  d2  e2    f2 
    5  1  0  3       N  1  N  x          5  1  0  3  1    x    --只中1个,中 1 
    2  0  4  5 -->  2  N  N  x  --->   2  0  4  5  2    y    --只中1个,中 2 
    4  0  3  2       N  N  3  x          4  6  3  2  4/3  y/x  --中了2个,中 3 中 4 
    2  1  8  7       4  N  N  y          2  1  8  7  2/1  x/x  --中了2个,中 2 中 1 
    5  4  7  7                              5  4  7  7              --一个都不中 
      

  2.   

        t1                t2                        t1 + t2 
    a  b  c  d      a1  b1  c1  d1        a2  b2  c2  d2  e2    f2 
    5  1  0  3      N  1  N  x          5  1  0  3  1    x    --只中1个,中 1 
    2  0  4  5 -->  2  N  N  x  --->  2  0  4  5  2    y    --只中1个,中 2 
    4  0  3  2      N  N  3  x          4  6  3  2  4/3  y/x  --中了2个,中 3 中 4 
    2  1  8  7      4  N  N  y          2  1  8  7  2/1  x/x  --中了2个,中 2 中 1 
    5  4  7  7                              5  4  7  7              --一个都不中 create table t1(a char,b char,c char,d char)
    insert into t1 values(5, 1, 0, 3)
    insert into t1 values(2, 0, 4, 5)
    insert into t1 values(4, 0, 3, 2)
    insert into t1 values(2, 1, 8, 7)
    insert into t1 values(2, 1, 8, 7)create table t2(a1 char,b1 char,c1 char,d1 char)
    insert into t2 values(N  1  N  x)
    insert into t2 values(2  N  N  x)
    insert into t2 values(N  N  3  x)
    insert into t2 values(4  N  N  y)
    go
      

  3.   

    create table t1(a varchar(10),b varchar(10),c varchar(10),d varchar(10)) 
    insert into t1 values('5', '1', '0', '3') 
    insert into t1 values('2', '0', '4', '5') 
    insert into t1 values('4', '0', '3', '2') 
    insert into t1 values('2', '1', '8', '7') 
    insert into t1 values('5', '4', '7', '7') 
    go
    create table t2(a1 varchar(10),b1 varchar(10),c1 varchar(10),d1 varchar(10)) 
    insert into t2 values('N' , '1' , 'N',  'x') 
    insert into t2 values('2',  'N' , 'N',  'x') 
    insert into t2 values('N',  'N',  '3',  'x') 
    insert into t2 values('4',  'N' , 'N',  'y') 
    go 
    select a2,b2,c2,d2,e2=case when len(e2)>0 then left(e2,len(e2)-1) else ''end,f2=case  when len(f2)>0 then left(f2,len(f2)-1) else ''end  from 
    (select a a2,b b2,c c2,d d2,
    e2=(case when a in (select a1 from t2) then a+'/' else '' end)+
    (case when b in (select b1 from t2) then b+'/' else '' end)+
    (case when c in (select c1 from t2) then c+'/' else '' end),
    f2=(case when a in (select a1 from t2) then (select d1 from t2 where a1=a)+'/' else '' end)+
    (case when b in (select b1 from t2) then (select d1 from t2 where b1=b)+'/' else '' end)+
    (case when c in (select c1 from t2) then (select d1 from t2 where c1=c)+'/' else '' end)
    from t1) aago
    drop table t1,t2
    /*
    a2         b2         c2         d2         e2                                f2                                
    ---------- ---------- ---------- ---------- --------------------------------- --------------------------------- 
    5          1          0          3          1                                 x
    2          0          4          5          2                                 x
    4          0          3          2          4/3                               y/x
    2          1          8          7          2/1                               x/x
    5          4          7          7                                            (所影响的行数为 5 行)*/
      

  4.   

    太强悍了,我发觉我越来越喜欢 sql 了,简单高效,现在才有这样的体会,接触得太迟了
    谢谢sdhdy ,dawugui !!