select * from 表 A
where not exists
(select 1 from 表 where picid=A.picid and provide=1 indatetime>A.indatetime and ID<A.ID)
where not exists
(select 1 from 表 where picid=A.picid and provide=1 indatetime>A.indatetime and ID<A.ID)
解决方案 »
- 不能还原数据库?
- 请教,“多增加一个(没必要的)字段,减少多表查询”的做法可取么?
- 尚学堂马士兵在讲解sql试题时似乎有误。望指教。
- 为什么我的查询语句没有问题.但是查不出来数据!
- 用SQL语句导入数据时触发器的问题(大力兄进来看看)
- sql2000server 中设计每个表的时候属性定义中的公式怎么用
- 桌面数据库与大型数据库的区别
- msde 安装失败 应用程序日志:注册表中性能计数器名称字符串数值的格式不正确?!怎么办?!
- 今天公司的两个客户的sql server一起出了毛病,疑惑中???
- 求教,事务的相关问题
- 我机子上的企业管理器进行任何操作,都会向C盘根目录下写一个SQL.LOG的文件,弄的很慢,怎么回事呀?
- 用域用户建立数据库,再用本地用户登录数据库,发现无法访问表是何原因?
select * from 表 A
where
indatetime=(select max(indatetime) from 表 where picid=A.picid and provide=1 )
--测试环境declare @t table(ID int,picid varchar(10),provide bit,defl varchar(200),indatetime datetime)
insert into @t select 40,'20014',1,'有现货(不再生产)','2005-11-3 11:24:00'
union all select 41,'20014',0,'款式不受欢迎','2005-11-2 11:24:05'
union all select 46,'19972',1,'现货提供(生产中)','2005-11-7 11:24:43'
union all select 47,'19972',0,'停产,不再继续供货','2005-11-8 11:24:48'
union all select 52,'19984',1,'现货提供(生产中)','2005-11-20 13:28:04'
--查询一
select * from @t A
where not exists
(select 1 from @t where picid=A.picid and indatetime>A.indatetime)
and
provide=1
--查询二
select * from @t A
where
indatetime=(select max(indatetime) from @t where picid=A.picid )
and provide=1 --结果ID picid provide defl indatetime
--------------------------------------------------------------------
40 20014 1 有现货(不再生产) 2005-11-03 11:24:00.000
52 19984 1 现货提供(生产中) 2005-11-20 13:28:04.000
ID picid provide defl indatetime
--------------------------------------------------------------------
40 20014 1 有现货(不再生产) 2005-11-03 11:24:00.000
52 19984 1 现货提供(生产中) 2005-11-20 13:28:04.000