求一条 sql 语句 (小弟积分不足先道歉了)

xuan195 2007-06-20 04:52:07

我想查询出数据表中 被使用的最多的个物品编号 只要求出该物品使用次数就可以了
最好是一条sql语句
表如下: 表id 物品id 使用时间
1 p2 null
2 p2 null
3 p1 null
4 p3 null
5 p1 null
...全文
154 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuan195 2007-06-20
  • 打赏
  • 举报
回复
楼上的 辛苦了
xuan195 2007-06-20
  • 打赏
  • 举报
回复
谢谢 各位
yrwx001 2007-06-20
  • 打赏
  • 举报
回复
create table #t( ID1 int,ID2 varchar(2),stime varchar(8))
insert #t
select 1,'p2',''
union all select 2,'p2',''
union all select 3,'p1',''
union all select 4,'p3',''
union all select 5,'p1',''
select * from #t

select top 1 ID2 from (select ID2,icount = count(ID2) from #t group by ID2)A order by icount desc


drop table #t
(影響 5 個資料列)

ID1 ID2 stime
----------- ---- --------
1 p2
2 p2
3 p1
4 p3
5 p1

(影響 5 個資料列)

ID2
----
p2

(影響 1 個資料列)
chuifengde 2007-06-20
  • 打赏
  • 举报
回复
select top 1 物品id,count(1) from [Table] group by 物品id order by 2
Zack999 2007-06-20
  • 打赏
  • 举报
回复
select top 1 物品id,count(物品id)as 使用次数 from 表
group by 物品id
order by count(物品id) desc
昵称被占用了 2007-06-20
  • 打赏
  • 举报
回复
select top 1 物品id ,count(*) as 使用次数
from tablename
group by 物品id
order by count(*) desc

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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