我在加入以下声明时遇到一些困难。
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**
yr_lease
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
感谢您提供的任何帮助。
答案 0 :(得分: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));
我不太确定在数据库性能方面哪个查询更好。