日期:2014-05-17 浏览次数:20969 次
select a.id, a.copmanyid, '失败' as status, a.createtime from yourtable a where not exists (select 1 from yourtable where copmanyid = a.copmanyid and id != a.id) --只存在一条记录 or (a.createtime = (select max(createtime) from yourtable where copmanyid = a.copmanyid) --最近一条记录 and not exists (select 1 from yourtable where copmanyid = a.copmanyid and status = '成功') --不存在成功记录 )
------解决方案--------------------
select t.id,t.companyid, '失败' as status,t.createtime from yourtable t where not exists (select 1 from yourtable t1 where t1.companyid = t.companyid and t1.id!=t.id) union all select t.id,t.companyid, '失败' as status,t.createtime from yourtable t where t.createtime = (select max(createtime) from yourtable t1 where t1.companyid=t.companyid) and not exists (select 1 from yourtable t2 where t2.companyid=t.companyid and status='成功')
------解决方案--------------------
select a.id, a.copmanyid, '失败' as status, a.createtime
from yourtable a
where not exists (select 1 from yourtable
where copmanyid = a.copmanyid and id != a.id) --只存在一条记录
or (a.createtime = (select max(createtime) from yourtable
where copmanyid = a.copmanyid) --最近一条记录
and not exists (select 1 from yourtable
where copmanyid = a.copmanyid
and status = '成功') --不存在成功记录
)
那位大神能给解释一下语句中的:select a.id, a.copmanyid, '失败' as status, a.createtime
from yourtable a。'失败' as status,红色的字段是什么意思?目的是什么?
------解决方案--------------------
'失败' as status 就是把两个汉字直接当作字段值进行返回,仅此而已。
比如:
select 'Hello' as xx, 'World' as oo from dual;
查询出来的结果就是两个字段,字段1是xx,字段2是oo