首先谢谢各位刚才的踊跃指导,刚刚的帖子小弟已经结贴给分了,
是我自己的原因,刚刚的帖子抽象出来的实例有点不太对头,以至于我都没法套用各位给的语句来测试一下,下面重新整理,请各位继续指教A 表 (客户信息表)
id, name
1 张三
2 李四B 表 (客户车辆保险续费表)
id aid time money
1 1 2008 1000
2 2 2008 1200
3 1 2009 1500
4 2 2009 1600C 表 (客户购车信息)
id aid carNumber
1 1 冀C9987
2 2 京P4489其中B,C表里面的aid对应A表id现在我想在不输入任何查询条件时示所有客户最后一次缴纳车辆保费的信息
name carNumber time money
张三 冀C9987 2009 1500
李四 京P4489 2009 1600也可以输入客户名称或车牌号进行查询具体的某一条记录,比如输入“张三”可查询出
name carNumber time money
张三 冀C9987 2009 1500谢谢各位大侠!
是我自己的原因,刚刚的帖子抽象出来的实例有点不太对头,以至于我都没法套用各位给的语句来测试一下,下面重新整理,请各位继续指教A 表 (客户信息表)
id, name
1 张三
2 李四B 表 (客户车辆保险续费表)
id aid time money
1 1 2008 1000
2 2 2008 1200
3 1 2009 1500
4 2 2009 1600C 表 (客户购车信息)
id aid carNumber
1 1 冀C9987
2 2 京P4489其中B,C表里面的aid对应A表id现在我想在不输入任何查询条件时示所有客户最后一次缴纳车辆保费的信息
name carNumber time money
张三 冀C9987 2009 1500
李四 京P4489 2009 1600也可以输入客户名称或车牌号进行查询具体的某一条记录,比如输入“张三”可查询出
name carNumber time money
张三 冀C9987 2009 1500谢谢各位大侠!
from a,b b1,c
where a.id = b1.aid
and a.id = c.aid
and not exists (
select 1 from b
where aid = b1.aid
and time > b1.time
)
-- and a.name ='张三' --这个条件在查询张三数据时使用
from a,b,c
where a.id=b.aid and a.id=c.aid
and not exists
(select 1 from b as tb where id=b.id and time>b.time)
--and name='张三'
select a.name,c.carNumber ,b.time ,b.money
from A a left join B b on a.id=b.aid
left join C c on a.id=c.aid
where not exists(select 1 from B bb where b.aid=bb.aid and b.time<bb.time)
SELECT A.NAME,C.CARNUMBER,B.TIME,B.MONEY
FROM A
LEFT JOIN (
SELECT TIME,MONEY FROM (
SELECT TIME,MONEY,ROW_NUMBER() OVER(PARTITION BY AID ORDER BY TIME DESC,ID DESC) NID FROM B
) T WHERE NID=1
) B ON A.ID=B.AID
LEFT JOIN C ON A.ID=C.AID
WHERE 1=1--条件自己爱加什么加什么了
insert a
select 1 ,'张三' union all
select 2 ,'李四'create table b(id int,aid int ,time varchar(4), money int)
insert b
select 1 ,1 ,'2008', 1000 union all
select 2 ,2 ,'2008', 1200 union all
select 3 ,1 ,'2009', 1500 union all
select 4 ,2 ,'2009', 1600create table c(id int,aid int,carNumber varchar(20))
insert c
select 1 ,1 ,'冀C9987' union all
select 2 ,2 ,'京P4489' select name,carNumber,time,money
from a,b,c
where a.id=b.aid and a.id=c.aid
and not exists
(select 1 from b as tb where aid=b.aid and time>b.time)
--and name='张三'/*
name carNumber time money
-------- -------------------- ---- -----------
张三 冀C9987 2009 1500
李四 京P4489 2009 1600(所影响的行数为 2 行)
*/
not exists (select 1 from b as tb where id=b.id and time>b.time)
我查了下 not exists 好像是如果后面这个查询语句不成立的时候的值返回来
但是“select 1 from b as tb where id=b.id and time>b.time” 这句中的 “time>b.time” 我始终不明白是怎么个意思? 这个是循环比对时间吗?我上面的“B 表 (客户车辆保险续费表)”虽然列的是一个客户有2条续保记录,但是也有可能是多条比如20条, 这样的话“time>b.time” 这个语句还好用吗?
create table #A --客户信息表
(id int, [name] nvarchar(10))
insert #A
select 1,'张三' union all
select 2,'李四'create table #B --客户保险续费表
(id int,aid int,[time] int, money int)
insert #B
select 1 ,1, 2008 ,1000 union all
select 2 ,2, 2008 ,1200 union all
select 3 ,1, 2009 ,1500 union all
select 4 ,2, 2009 ,1600create table #C --客户购车信息
(id int, aid int, carNumber nvarchar(20))
insert #c
select 1 ,1, '冀C9987' union all
select 2 ,2, '京P4489'declare @name nvarchar(10)
declare @carNumber nvarchar(20)
set @name='' --改这里,传 '' 就全部查出来..传'张三'就查张三的
set @carNumber='' --改这里
select isnull(a.[name],'') as [name],isnull(c.carNumber,'') as carNumber,b.[time],b.money from #B as b
Left join #A as a on a.id=b.aid
Left join #C as c on a.id=c.aid
where not exists(select 1 from #B as b1 where b1.aid=b.aid and b1.[time]>b.[time])
and (@name='' or (@name<>'' and a.[name]=@name))
and (@carNumber='' or (@carNumber<>'' and c.carNumber=@carNumber))--name carNumber time money
------------ -------------------- ----------- -----------
--张三 冀C9987 2009 1500
--李四 京P4489 2009 1600
--
--(2 row(s) affected)