日期:2014-05-16  浏览次数:20700 次

这个视图怎么写?
一个物业表,
id,wyF1,wyF2......
一个租赁表
id,wyID,zlF1,zlF2......

一个物业基本信息是固定的,但是对应的租赁信息可能有很多条,在不同的时间有不同的租赁人信息

要一个视图
一个物业表的所有字段和最新租赁人的相关信息(根据租赁时间来确定最新租赁人)

这个视图怎么写?

------解决方案--------------------
create view v_test
as
select A.*,D.*
from 物业表 A,(select * from 租赁表 B where not exists (select 1 from 租赁表 C where B.租赁人=C.租赁人 and B.租赁时间<C.租赁时间))D
where A.id = D.wyID;
------解决方案--------------------
贴建表及插入记录的SQL,及要求结果出来看看

try:
select * from 物业表 c inner join (
select * from 租赁表 a where not exists(select 1 from 租赁表 where a.wyID=wyID and a.时间<时间)) d
on c.id=d.wyI
------解决方案--------------------
SQL code
create veiw xxxxx 
as
select a.*,b.*
from 物业表 a,租赁表 b,(select wyID,max(id) as lid from 租赁表 group by wyID) c
where a.id=c.wyID and b.wyID=c.wyID

------解决方案--------------------
参考下贴中的多种方法

http://topic.csdn.net/u/20091231/16/2f268740-391e-40f2-a15e-f243b2c925ab.html
[征集]分组取最大N条记录方法征集,及散分....
------解决方案--------------------
SQL code
select * from 物业表 a inner join (
select * from 租赁表 a where not exists(select 1 from 租赁表 where a.wyID=wyID and a.时间<时间)) b
on a.id=b.wyI