这样的sql语句如何写啊?急

blueye 2003-09-13 01:13:49
一个表中有2个列a(num(1))和b(num(3)),我要选择出a=1 所有数据中b字段中数字最大的记录
...全文
38 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
rainbow0326 2003-09-13
  • 打赏
  • 举报
回复
支持 yoki(小马哥) 的回答,我試過了
yujohny 2003-09-13
  • 打赏
  • 举报
回复
select * from table1
where b = (select max(b) from table1 where a='1') and a='1'
wzh1215 2003-09-13
  • 打赏
  • 举报
回复
insert into #aa
select * from talbe1 where a='1'
select max(b) from #aa
yoki 2003-09-13
  • 打赏
  • 举报
回复
select * from table1
where b in(select top 1 b from table1 where a='1' order by b desc)
and a='1'
yoki 2003-09-13
  • 打赏
  • 举报
回复
select * from table1
where b in(select top 1 b from table1 where a='1' order by b desc)
rainbow0326 2003-09-13
  • 打赏
  • 举报
回复
TO yyy431706(共同进步)
你的查詢語句後面還是要加上and a='1',否則查到的會a=其它的東東
試驗的結果:)
szleopard 2003-09-13
  • 打赏
  • 举报
回复
引用(共同进步)的回复
select * from 表名 where B IN (select max(B) from 表名 where a=1)

这个应该是最简单的,而且目的也能达到。但是如果数据量大的话,查询的速度可能会慢点。
yyy431706 2003-09-13
  • 打赏
  • 举报
回复
select * from 表名 where B IN (select max(B) from 表名 where a=1)

这个也行,yoki(小马哥)的:
select * from 表名
where B in(select top 1 B from 表名 where A='1' order by B desc)
and A='1'
blueye 2003-09-13
  • 打赏
  • 举报
回复
但是显示在web页面上时这条记录前面会有若干空行,就是被滤到的一些记录,请问如何解决
panyee 2003-09-13
  • 打赏
  • 举报
回复
select top 1 * from table where a='1' order by b desc
blueye 2003-09-13
  • 打赏
  • 举报
回复
我要选择的是先是A=1出来的数据中b再最大的那条数据?你给我的语句是按我的顺序执行的嘛?
cgsun 2003-09-13
  • 打赏
  • 举报
回复
select max(b) from table where a='1'

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧