求一SQL语句

wdx2010 2010-12-03 10:06:45
select top 10 id,price,title,photo,city from test
要求取出不同城市价格最低的最新10条记录

伪SQL:
select top 10 id,price,title,photo,city from test group by city order by price desc
...全文
200 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
子夜__ 2010-12-03
  • 打赏
  • 举报
回复
如果城市有重复的 用游标可否?
dicklee1214 2010-12-03
  • 打赏
  • 举报
回复
用存储过程似乎比较容易,取不同的(distinct)城市ID,打开游标,动态循环拼装(union all)select top 10 id,price,title,photo,city from test order by price desc这样的一段sql,存储过程返回执行拼装后的sql的查询结果集!
打一壶酱油 2010-12-03
  • 打赏
  • 举报
回复
不清楚你是一共 取十条, 还是 每个城市 都取 十条
仙儿 2010-12-03
  • 打赏
  • 举报
回复
select top 10 max(id),min(price),city from test group by city order by price asc,time desc
wdx2010 2010-12-03
  • 打赏
  • 举报
回复
按城市分组 city in(..)这样也不行,city换是有重复的
select max(id) from test group city 也不行,这样得到的不是最低价格的id
select min(price) from test group city 也不行..

以前写过忘记了,貌似很麻烦啊
wdx2010 2010-12-03
  • 打赏
  • 举报
回复
第一个正确给出的得满分!

select top 10 id,price,title,photo,city from test group by city order by price asc,time desc
这个是不对的,多个字段必须按照多个字段分组,这样城市就有重复的了。。
一切为了你 2010-12-03
  • 打赏
  • 举报
回复
按城市分组 按价格排序
q107770540 2010-12-03
  • 打赏
  • 举报
回复
select top 10 id,price,title,photo,city from test group by city order by price asc,time desc
wdx2010 2010-12-03
  • 打赏
  • 举报
回复
谢谢大家
lilianjie0426 2010-12-03
  • 打赏
  • 举报
回复
15楼的就可以。
xf2711 2010-12-03
  • 打赏
  • 举报
回复
select top 10 id,price,title,photo,city from test _out and not exists(select 1 from test where city=_out.city and price<_out.price and id > _out.id) order by price asc
wangderong8389945 2010-12-03
  • 打赏
  • 举报
回复
select top 10 min(price),city from test group by city,price
order by price
这个应该可以的!!!
xf2711 2010-12-03
  • 打赏
  • 举报
回复
select top 10 id,price,title,photo,city from test _out where price = (select min(price) from test where city=_out.city) and not exists(select 1 from test where city=_out.city and price=_out.price and id > _out.id)
打一壶酱油 2010-12-03
  • 打赏
  • 举报
回复
前面搞错了 排序
select top 10 id,price,title,photo,city from test a
where not exists(select 1 from test b where b.city = a.city and b.price < a.price )
order by price
打一壶酱油 2010-12-03
  • 打赏
  • 举报
回复
15楼的可以,我的也可以

select top 10 id,price,title,photo,city from test a
where not exists(select 1 from test b where b.city = a.city and b.price < a.price )
order by price desc
J_HongHai 2010-12-03
  • 打赏
  • 举报
回复
select top 10 min(price),city from test group by city order by price,time desc
PitTroll 2010-12-03
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 takako_mu 的回复:]
select top 10 * from(
select min(price) as price,city from tb group by city
) order by price
[/Quote]
这个应该可以满足吧
v风雪山神庙v 2010-12-03
  • 打赏
  • 举报
回复
是不是同一个城市的价格可能有多个?
如果这样的话,用子表查询

takako_mu 2010-12-03
  • 打赏
  • 举报
回复
select top 10 * from(
select min(price) as price,city from tb group by city
) order by price
wdx2010 2010-12-03
  • 打赏
  • 举报
回复
就是取价格最低的前十条,要求城市不重复
加载更多回复(4)

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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