求一条查询语句!

ykren 2007-06-06 01:53:02
PhotoID ShopID PhotoName PhotoDefault AddDate
----------- ----------- ---------------- ------------ --------------------
8 76 4164022546.jpg 0 2007-06-04 16:40:00
9 53 4164215171.jpg 0 2007-06-04 16:42:00
10 53 4164612343.jpg 1 2007-06-04 16:46:00
11 53 4164655671.jpg 0 2007-06-04 16:47:00
12 53 417650828.jpg 0 2007-06-04 17:07:00
13 3 4223634562.jpg 0 2007-06-04 22:37:00
14 76 422395162.jpg 0 2007-06-04 22:40:00

ShopID中相同的数值只保留一条,满足保留的条件为:PhotoDefault为最大值,或者AddDate为最大值。
...全文
168 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
ykren 2007-06-06
  • 打赏
  • 举报
回复
不能用order by,我这条语句是作为子句用的。
ykren 2007-06-06
  • 打赏
  • 举报
回复
我要的结果应该是这样的:

PhotoID ShopID PhotoName PhotoDefault AddDate
----------- ----------- ---------------- ------------ --------------------
10 53 4164612343.jpg 1 2007-06-04 16:46:00
13 3 4223634562.jpg 0 2007-06-04 22:37:00
14 76 422395162.jpg 0 2007-06-04 22:40:00
hxd001_810 2007-06-06
  • 打赏
  • 举报
回复
条件为:PhotoDefault为最大值
select * from a b
where not exists(select 1 from a where shopid=b.shopid and
PhotoDefault>b.PhotoDefault)
order by b.ShopID

条件为:AddDate为最大值
select * from a b
where not exists(select 1 from a where shopid=b.shopid and
adddate>b.adddate)
order by b.ShopID
ykren 2007-06-06
  • 打赏
  • 举报
回复
你给的语句我测试的结果是这样的:

PhotoID ShopID PhotoName PhotoDefault AddDate
----------- ----------- ---------------- ------------ --------------------
12 53 417650828.jpg 0 2007-06-04 17:07:00
13 3 4223634562.jpg 0 2007-06-04 22:37:00
14 76 422395162.jpg 0 2007-06-04 22:40:00
ykren 2007-06-06
  • 打赏
  • 举报
回复
jinjazz(近身剪) :查询结果和我要的有点出入。我要的结果是如果PhotoDefault为1的话,那么重复记录就显示这条PhotoDefault为1的记录,如果PhotoDefault为0的话那么就取AddDate最大值。
xiyongjie 2007-06-06
  • 打赏
  • 举报
回复
就是DISTINCT
xiyongjie 2007-06-06
  • 打赏
  • 举报
回复
上面的还要加一个关键字才能控制相同的只显示一条纪录
jinjazz 2007-06-06
  • 打赏
  • 举报
回复
select * from table a
where not exists(select 1 from table where shopid=a.shopid and
adddate>a.adddate)

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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