两张表A:
编号 名称 
1     nameB:
编号1 编号2 内容
1     1     xxx
2     1     xxy
3     1     xxZA.编号 = B.编号2问:用一条SQL语句从两个表中取出第一条记录?
我写的:select * from A ,B where a.编号=b.编号2 and (a.name = 'name' or b like 'xx%'),这一条会列出三条记录。只要求列出一条,咋办?谢谢!

解决方案 »

  1.   

    加上TOP1试试:
    select TOP 1 * from A ,B where a.编号=b.编号2 and (a.name = 'name' or b like 'xx%')
      

  2.   

    如果表A中有多条记录,B中有更多条记录与表A中的记录相关,要求是:列出A表中的所有和B表中第一条符合条件的数据。这样的SQL?
      

  3.   

    B表中编号2相同的行中取编号1最小的那行.
    select a.*,b.* from tA as a left join tB as b on a.编号 = b.编号2
    where not exists(select 1 from tB where 编号2 = b.编号2 and 编号1 < b.编号1)
      

  4.   

    okselect TOP 1 * from A ,B where a.编号=b.编号2 and (a.名称= 'name' or b.内容 like 'xx%')
      

  5.   

    select top 1 * from A ,B where a.[编号]=b.[编号2] and (a.[名称] = 'name' or b.[内容] like 'xx%')这下可以吗?或将要的结果集列出来