1

我在加入以下陈述时遇到了一些困难。

Select *
From yr_property
Left Join yr_lease on yr_lease.propertynum = yr_property.propertynum
Where yr_lease.leasenum is null;



Select Dateto
From   yr_advert;

我已经尝试加入表格,但我认为并非一切都正确

left join yr_advert on yr_advert.propertynum = yr_property.propertynum

我的目标是...

列出未出租物业,并注明其最后一次发布广告的日期(最后一次广告停止的日期)。

使用中的表

yr_property

Propertynum , CHAR(5), NOT nullable (PRIMARY KEY 1) 
Street , VARCHAR(25),      nullable   
Area , VARCHAR2(15),       nullable 
City , VARCHAR2(15),       nullable 
Postcode , VARCHAR2(8),    nullable 
Prop_type , VARCHAR2(15),  nullable 
Rooms NUMBER , (2,0),      nullable 
Staffnum , CHAR(5),        nullable
Branchnum , CHAR(3)        nullable   
Ownernum , CHAR(5)         nullable
Comments , VARCHAR2(500)   nullable
Date_ended , DATE          nullable   **FORMAT EG - 11/JAN/2004**

年租约

Leasenum , CHAR(5),          NOT nullable (PRIMARY KEY 1) 
Renternum, ,  CHAR(5),       nullable   
Propertynum , CHAR(5),       nullable (FOREIGN KEY)
Paymentmethod , VARCHAR2(15),nullable 
Rent , NUMBER(17,2),         nullable 
Depositamount , NUMBER(15),  nullable 
Depositpaid , VARCHAR2(5),   nullable
Rentstart , DATE             nullable 
Rentfinish , DATE            nullable    **FORMAT EG - 11/JAN/2004**

yr_advert

ADVERTNUM ,    CHAR(10),    NOT nullable (PRIMARY KEY 1)
PROPERTYNUM ,  CHAR(5),     NOT nullable (PRIMARY KEY 2)
STAFFNUM ,  CHAR(5),          nullable
TOAPPEAR ,  VARCHAR2(100),    nullable
DATEFROM ,  DATE,             nullable  
DATETO ,  DATE ,              nullable   **FORMAT EG - 11/JAN/2004**
WORDS,  VARCHAR2(500),        nullable

感谢您为我提供的任何帮助。

4

1 回答 1

1

此查询用于查找在其最后一个广告期未使用的属性。

SELECT *
FROM yr_property A
INNER JOIN yr_advert B ON A.propertynum = B.propertynum
LEFT JOIN yr_lease C ON B.propertynum = C.propertynum AND (C.rentstart BETWEEN B.datefrom AND B.dateto OR C.rentfinish BETWEEN B.datefrom AND B.dateto)
WHERE B.dateto = (SELECT MAX(D.dateto) FROM yr_advert D WHERE A.propertynum = D.propertynum)
  AND C.leasenum IS NULL;

替代查询:

SELECT *
FROM yr_property A
INNER JOIN yr_advert B ON A.propertynum = B.propertynum
WHERE B.dateto = (SELECT MAX(C.dateto) FROM yr_advert C WHERE A.propertynum = C.propertynum)
  AND NOT EXISTS (SELECT 1 FROM yr_lease D WHERE A.propertynum = D.propertynum AND (D.rentstart BETWEEN B.datefrom AND B.dateto OR D.rentfinish BETWEEN B.datefrom AND B.dateto));

我不太确定在数据库性能方面哪个查询更好。

于 2013-04-28T17:51:03.027 回答