已知两张表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更大的中的最大项该怎么写?
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更大的中的最大项该怎么写?
解决方案 »
- MSSQL有没有像oracle一样的性能分析工具?
- SQL Server2005 数据库作业中 如何用ActiveX脚本定时打开指定的页面呢?
- SQL Server Compact Edition 能像 MSDE 一样使用吗?
- 如何用sqlserver存储过程或触发器生成一个文本文件
- windows 验证方式??
- 请大家出手,求一组MS SQL语句
- access转成MSSQL,备注字段内容丢失,怎么办
- 错误类型:Microsoft OLE DB Provider for ODBC Drivers (0x80004005)
- 不允许程序创建字段的时候如何实现实体属性的动态扩展
- 可以介绍一下吗?ORCALE里的DECODE的用法与例子?
- PowerDesigner怎么找到英文的表名
- 向晴天老师请教!
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
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
AND TableA.NAME='已知NAME' AND TableB.project>'已知project'
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
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是可以查询的,但是我要的是要是有更大的在几条结果中的最大的那项。请问该怎么改啊!
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
直接加max不行啊,说是“因为该列没有包含在聚合函数或 GROUP BY 子句中”