select a.*,(select top 1 b.* from b where ID=a.ID) from a

解决方案 »

  1.   

    select *,(select top 1 * from B where id=A.id) from A
      

  2.   

    有错误:
    当没有用 EXISTS 引入子查询时,在选择列表中只能指定一个表达式。
      

  3.   

    select a.*, b.* from a right join b 
        on a.id = b.id
        where b.id in( 1,2,3)right join返回b表的所有记录.
      

  4.   

    --没错啊,你看测试:declare @A1 table(id int)
    insert into @A1
    select 1
    union all select 2
    union all select 3
    union all select 4declare @B1 table(id int)
    insert into @b1
    select 1
    union all select 1
    union all select 2
    union all select 2
    union all select 2
    union all select 2
    union all select 3
    union all select 3
    union all select 3
    union all select 1
    union all select 2select *,(select top 1 * from @b1 where a.id=id) from @a1 a
      

  5.   

    --如果B1表中还包含其他字段,就改用临时表.--创建临时表
    select myid=identity(int,1,1),* into #tb from B1--查询结果
    select * from A1 a inner join #tb b on a.id=b.id
    and b.myid=(select min(myid) from #tb where id=a.id)--删除临时表
    drop table #tb
      

  6.   

    --下面是数据测试:declare @A1 table(id int,a int)
    insert into @A1
    select 1,22
    union all select 2,33
    union all select 3,44
    union all select 4,55declare @B1 table(id int,b int)
    insert into @b1
    select 1,3
    union all select 1,4
    union all select 2,5
    union all select 2,6
    union all select 2,7
    union all select 2,8
    union all select 3,9
    union all select 3,10
    union all select 3,11
    union all select 1,12
    union all select 2,13select myid=identity(int,1,1),* into #tb from @b1select * from @a1 a inner join #tb b on a.id=b.id
    and b.myid=(select min(myid) from #tb where id=a.id)drop table #tb/*--测试结果id          a           myid        id          b           
    ----------- ----------- ----------- ----------- ----------- 
    1           22          1           1           3
    2           33          3           2           5
    3           44          7           3           9(所影响的行数为 3 行)
    --*/
      

  7.   

    A,B两表都有其他字段,请问一句SQL能求出想要的记录集吗?
      

  8.   


    select a.*,b.*  into #tep from a,b where a.id=b.id 
    select top 1* from #tep
      

  9.   


    select top 1 a.*, b.*    from a,b where a.id=b.id
      

  10.   

    回复人: cbzdream(迷茫) ( ) 信誉:90  2003-10-17 11:14:00  得分:0 
     
     
      有错误:
    当没有用 EXISTS 引入子查询时,在选择列表中只能指定一个表达式。
    ---------------------------------------------------------------------
    select a.* ,(select top 1 表b的第一列 from b where a.id = b.id), ....,(select top 1 表b的最后一列 from b where a.id = b.id) from a表b中的列很多时,可动态生成上面的语句!