关于sql语句的写法
时间:2011-12-22
来源:互联网
SQL code
f1 listdate remark c 2011-12-12 a 2011-12-02 123 b 2011-11-01 44rr4 c 2011-12-01 774 c 2011-12-09 oiiie2 d 2011-12-2 a 2011-11-02 234
我想取当listdate最大的日期,取remark的数据,并且remark不能为空
SQL code
f1 listdate remark a 2011-12-02 123 b 2011-11-01 44rr4 c 2011-12-09 oiiie2
注意:不能取f1=c、listdate=2011-12-12的数据,因为remark为空的。
如何写sql语句?
作者: fstao 发布时间: 2011-12-22
select * from #t1 a where remark is not null --如果不为空串,则 remark<>'' 下同 and not exists(select 1 from #t1 where f1=a.f1 and remark is not null and listdate>a.listdate)
作者: qianjin036a 发布时间: 2011-12-22
select * from #t1 t where remark is not null and not exists(select 1 from #t1 where f1=t.f1 and remark is not null and listdate>t.listdate)
作者: fredrickhu 发布时间: 2011-12-22
SQL code
select *
from #t1 a
where remark is not null --如果不为空串,则 remark<>'' 下同
and not exists(select 1 from #t1 where f1=a.f1 and remark is not null and listdate>a.listdate)
作者: szstephenzhou 发布时间: 2011-12-22
create table #t1 (f1 char(3), listdate date, remark varchar(9)) insert into #t1 select 'c', '2011-12-12', '' union all select 'a', '2011-12-02', '123' union all select 'b', '2011-11-01', '44rr4' union all select 'c', '2011-12-01', '774' union all select 'c', '2011-12-09', 'oiiie2' union all select 'd', '2011-12-2', '' union all select 'a', '2011-11-02', '234' select a.* from #t1 a inner join (select f1,max(listdate) maxlistdate from #t1 where remark<>'' group by f1) b on a.f1=b.f1 and a.listdate=b.maxlistdate f1 listdate remark ---- ---------- --------- a 2011-12-02 123 b 2011-11-01 44rr4 c 2011-12-09 oiiie2 (3 row(s) affected)
作者: ap0405140 发布时间: 2011-12-22
SQL code
create table #t1
(f1 char(3),
listdate date,
remark varchar(9))
insert into #t1
select 'c', '2011-12-12', '' union all
select 'a', '2011-12-02', '123' union all
select 'b……
mark
作者: ju523756055 发布时间: 2011-12-22
作者: dawugui 发布时间: 2011-12-22
select t.* from tb t where remark is not null and listdate = (select max(listdate) from tb where f1 = t.f1 and remark is not null)
select t.* from tb t where remark is not null and not exists (select 1 from tb where f1 = t.f1 and remark is not null and listdate > t.listdate)
作者: dawugui 发布时间: 2011-12-22
if object_id('tb') is not null drop table tb go create table tb ( f1 varchar(10), listdate varchar(10), remark varchar(10) ) go insert into tb select 'c','2011-12-12','' union all select 'a','2011-12-02','123' union all select 'b','2011-11-01','44rr4' union all select 'c','2011-12-01','774' union all select 'c','2011-12-09','oiiie2' union all select 'd','2011-12-2','' union all select 'a','2011-11-02','234' go select * from tb a where remark<>'' and not exists(select 1 from tb where f1=a.f1 and listdate>a.listdate and remark<>'') go /* f1 listdate remark ---------- ---------- ---------- a 2011-12-02 123 b 2011-11-01 44rr4 c 2011-12-09 oiiie2 (3 行受影响) */
作者: pengxuan 发布时间: 2011-12-22
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28