select * from 表 a where not exists(select * from 表 where phone=a.phone,code=a.code,content=a.content,time>a.time)
a表和b表什么关系?为什么不直接从b表里取?
select * from 表 a where not exists(select * from 表 where phone=a.phone,time>a.time)
晕,低级错误~把上面的逗号改成and select * from 表 a where not exists(select * from 表 where phone=a.phone and time>a.time)
没说清楚,不好意思 A表 phone code 111 aa 111 bb 222 bb B表 phone code content time 111 aa fasdf 2006-04-10 111 aa sdfss 2006-04-11 222 bb ddda 2006-04-12 111 bb fdsfs 2006-04-13 根据表a, 111 aa sdfss 2006-04-11 111 bb fdsfs 2006-04-13 222 bb ddda 2006-04-12 sql怎么写啊
declare @t table(phone varchar(6), code varchar(6)) declare @t1 table(phone varchar(6), code varchar(6) , content varchar(10), time datetime) insert into @t select '111', 'aa' union select '111', 'bb' union select '222', 'bb' insert into @t1 select '111', 'aa', 'fasdf', '2006-04-10' union select '111', 'aa', 'sdfss', '2006-04-11' union select '222', 'bb', 'ddda', '2006-04-12' union select '111', 'bb', 'fdsfs', '2006-04-13'select * from @t1 a where a.time in ( select max(time) from @t1 b where b.phone = a.phone and b.code = a.code )不知道可以满足楼主的要求吗
select * from 表 a where not exists(select * from 表 where phone=a.phone and time>a.time)
A表
phone code
111 aa
111 bb
222 bb
B表
phone code content time
111 aa fasdf 2006-04-10
111 aa sdfss 2006-04-11
222 bb ddda 2006-04-12
111 bb fdsfs 2006-04-13
根据表a,
111 aa sdfss 2006-04-11
111 bb fdsfs 2006-04-13
222 bb ddda 2006-04-12
sql怎么写啊
declare @t1 table(phone varchar(6), code varchar(6) , content varchar(10), time datetime)
insert into @t
select '111', 'aa' union
select '111', 'bb' union
select '222', 'bb' insert into @t1
select '111', 'aa', 'fasdf', '2006-04-10' union
select '111', 'aa', 'sdfss', '2006-04-11' union
select '222', 'bb', 'ddda', '2006-04-12' union
select '111', 'bb', 'fdsfs', '2006-04-13'select *
from @t1 a
where a.time in
(
select max(time) from @t1 b
where b.phone = a.phone and b.code = a.code
)不知道可以满足楼主的要求吗
不行啊 ,好像@t都没用到阿
zjdyzwx(十一月猪)