已知两张表TableA,TableB.
TableA: a_id(int), name(nvarchar(20)), time(datetime);
TableB: b_id(int), a_id(int), project(int), time(datetime);其中TableB中的a_id和TableA中的a_id是外键关系。
TableB中a_id和project为主键要是TableA的name和TableB中的project都知道,想查询有没有比已知的project更大的中的最大项该怎么写?

解决方案 »

  1.   


    select top 1 * from TableA a ,TableB b where a.a_id=b.a_id
    and a.name=@name and b.project>@project
    order by b.project desc 
      

  2.   


    declare @name nvarchar(20),@project int
    set @name='yourname'
    set @project=100
    select project from tableb a
    where a_id=(select top 1 a_id from tablea where name=@yourname)
    and project>@project
      

  3.   

    SELECT * FROM TableA,TableB WHERE TableA.a_id=TableB.a_id
    AND TableA.NAME='已知NAME' AND TableB.project>'已知project'
      

  4.   

    select
      a.*
    from
      tablea a,tableb b
    where
      a.a_id=b.a_id
    and
      a.project=(select max(project) from tablea where a_id=a.a_id and name=a.name)
    and
      a.project>b.project
      

  5.   

    declare @name nvarchar(20),@project int
    set @name='yourname'
    set @project=100
    select project from tableb a
    where a_id=(select top 1 a_id from tablea where name=@yourname)
    and project>@project是可以查询的,但是我要的是要是有更大的在几条结果中的最大的那项。请问该怎么改啊!
      

  6.   


    declare @name nvarchar(20),@project int
    set @name='yourname'
    set @project=100
    select max(project) from tableb a
    where a_id=(select top 1 a_id from tablea where name=@yourname)
    and project>@project
      

  7.   


    直接加max不行啊,说是“因为该列没有包含在聚合函数或 GROUP BY 子句中”