A、B  
有两个表        
   A:        
colunm1                    colunm2            colunm3    
a1                          2                        1        
b1                          4                        2        
a1                          5                        3        
f1                          6                        1        
b1                          2                        8   B:        
colunm1                 colunm2      
a1                          8        
b1                          11
e2                          9    
         
   能用查到这样一个结果吗        
         
colunm1              colunm2            colunm3          colunm4  
a1                      2                  1                 8  
b1                      4                  2                11  
a1                      5                  3                null  
f1                      6                  1                null 
b1                      9                  8                nullA,B表,只有colunm1有关联,c其他字段都没有一定的规律和大小关系,请教大家如何解决。前天写过可能是表数据引起大家的误解,不好意思,现在急需大家帮忙。谢谢

解决方案 »

  1.   

    select a.* , b.colunm1 from A left join B on a.column1=b.column1
      

  2.   

    如果用这语句,执行结果会是这样的
    colunm1              colunm2            colunm3          colunm4  
    a1                      2                  1                 8  
    b1                      4                  2                11  
    a1                      5                  3                 8   
    f1                      6                  1                null 
    b1                      9                  8                11
      

  3.   

    select a.* , b.colunm2  as colunm4  
    from A left join B on a.column1=b.column1
      

  4.   

    select t.* , 
    column4=case when exists(select 1 from a where column1=t.column1 and column3<t.column3 ) then null else b.colunm2 end
    from A t left join B on t.column1=b.column1
      

  5.   

    select a.* , b.colunm1 from A left join B on a.column1=b.column1
      

  6.   

    楼主给的数据又点偏差,如果按照结果是正确的话,A表中最后一条数据应该是b1                          9                        8对吧?
    select a.colunm1,a.colunm2,a.colunm3 ,d.colunm4 from a left outer join (
    select a.colunm1,a.colunm2,a.colunm3,b.colunm2 as colunm4 from a inner join b  on a.colunm1=b.colunm1
     where exists (select 1  from a c  where  c.colunm1=a.colunm1 and c.colunm2>a.colunm2))d
    on a.colunm1=d.colunm1 and  a.colunm2=d.colunm2 and  a.colunm3=d.colunm3--结果
    a1 2 1 8
    a1 5 3 NULL
    b1 9 8 NULL
    b1 4 2 11
    f1 6 1 NULL
      

  7.   

    try:
    CREATE TABLE #T1(colunm1 nvarchar(20),colunm2 int,colunm3 int) 
    INSERT INTO #T1  
    SELECT 'a1',2,1 UNION ALL   
    SELECT 'b1',4,2 UNION ALL           
    SELECT 'a1',5,3 UNION ALL           
    SELECT 'f1',6,1 UNION ALL           
    SELECT 'b1',2,8CREATE TABLE #T2(colunm1 nvarchar(20),colunm2 int)    
    INSERT INTO #T2  
    SELECT 'a1',8  UNION ALL          
    SELECT 'b1',11 UNION ALL   
    SELECT 'e2',9    SELECT IDENTITY(int,1,1) AS [id],* INTO #T FROM #T1
    SELECT A.*,B.colunm2 AS colunm4 FROM #T AS A  
    LEFT OUTER JOIN #T2 AS B ON B.colunm1=A.colunm1
    AND NOT EXISTS(SELECT 1 FROM #T AS C WHERE C.colunm1=A.colunm1 AND C.[id]<A.[id])
    DROP TABLE #T1,#T2,#T
    /*
    id colunm1 colunm2 colunm3 colunm4
    ---------------------------------------
    1 a1 2 1 8
    2 b1 4 2 11
    3 a1 5 3 NULL
    4 f1 6 1 NULL
    5 b1 2 8 NULL
    */
    --只要A表有一个唯一标志的自增ID就好办了,不然得使用临时表新建一个唯一标志的自增ID。
      

  8.   

    DVD_01的方法应该是可行的,再试试,感谢大家的支持,谢谢。
      

  9.   

    --建立測試環境
    declare @A table(colunm1 varchar(20),colunm2 varchar(20),colunm3 int)
    insert @A select 
    'a1' ,'2',           1  union all select   
    'b1' ,'4',           2  union all select    
    'a1' ,'5',           3  union all select    
    'f1' ,'6',           1  union all select       
    'b1' ,'2',           8   
    declare @b TABLE(colunm1 VARCHAR(20),colunm2 VARCHAR(20))
    INSERT @b SELECT 
    'a1', 8  union all select      
    'b1', 11  union all select 
    'e2', 9 --查詢
    SELECT a.colunm1,a.colunm2,a.colunm3,case when RowNo=1 then b.colunm2 else null end as colunm4
     from (select *,RANK() OVER(partition by colunm1 order by colunm1,colunm2,colunm3) as RowNo from @A) a
     left join @B b on a.colunm1=b.colunm1 /*
    結果:
    colunm1              colunm2              colunm3     RowNo                colunm4
    -------------------- -------------------- ----------- -------------------- --------------------
    a1                   2                    1           1                    8
    a1                   5                    3           2                    NULL
    b1                   2                    8           1                    11
    b1                   4                    2           2                    NULL
    f1                   6                    1           1                    NULL
    */--應該沒有此記錄
    --b1                      9                  8                null
      

  10.   

    /*
    結果:
    colunm1              colunm2              colunm3     colunm4
    -------------------- -------------------- ----------- --------------------
    a1                   2                    1           8
    a1                   5                    3           NULL
    b1                   2                    8           11
    b1                   4                    2           NULL
    f1                   6                    1           NULL
    */
      

  11.   

    sele a.column1 as column1, a.column2 as column2, a.column3 as column3, b.column2 as column4 from at a left join bt b on a.column1=b.column1结果不对的说
      

  12.   

    select a.*,b.colunm1 from a,b where a.colunm1=b.clouun1(*)
      

  13.   

    着不就是左连接吗?就今天上午学过。
    不过没有说具体SQL语言
      

  14.   

    select B.column1,(select top 1 column2 from A where column1=b.column1 order by column2,column3) as column2,
       (select top 1 column2 from A where column1=b.column1 order by column2,column3) as column3,b.column2 as column4
    from b where exists(select * from A where a.column1=b.column1) 
    用A表与以上查询结果进行左连接,on的条件为column1,column2,column3即可得到结果
      

  15.   

    select a.column1,a.column2,a.column3,b.column2 from a,b where a.column1=b.column.1
      

  16.   

    如果不用column1来观念,建立另外一个字段关联是不是更简单呢?