模式
Tenants (HouseID, ApartmentNumber, LeaseTenantSSN, LeaseStartDate(not null), LeaseExpirationDate(can be null),
Rent, LastRentPaidDate, RentOverdue)
NewRentPayments (HouseID, ApartmentNumber, LeaseTenantSSN(not null), Rent(can be null), DatePaid)
查询
INSERT INTO Tenants (x.HouseID,x.ApartmentNumber,x.LeaseTenantSSN,CURRENT_DATE,NULL,x.Rent,x.Rent,FALSE)
SELECT x.HouseID, x.ApartmentNumber,x.LeaseTenantSSN,CURRENT_DATE,NULL,x.Rent,x.Rent
FROM NewRentPayments x
WHERE x.HouseID NOT IN (select HouseID
FROM Tenants)
OR x.ApartmentNumber NOT IN (SELECT ApartmentNumber
FROM Tenants
WHERE Tenants.HouseID=x.HouseID);
我总是在CURRENT_DATE得到一个错误,NULL。它在CURRENT_DATE或附近说sintax错误
答案 0 :(得分:2)
您的语法已关闭。以下INSERT INTO
应该是您计划插入数据的列名列表,使用以下SELECT
作为源。这样的事情应该有效:
INSERT INTO Tenants (HouseID, ApartmentNumber, LeaseTenantSSN, LeaseStartDate,
LeaseExpirationDate, Rent, LastRentPaidDate, RentOverdue)
SELECT x.HouseID, x.ApartmentNumber, x.LeaseTenantSSN, CURRENT_DATE, NULL, x.Rent,
x.DatePaid, FALSE
FROM NewRentPayments x
WHERE x.HouseID NOT IN (SELECT HouseID FROM Tenants) OR
x.ApartmentNumber NOT IN (SELECT ApartmentNumber FROM Tenants
WHERE Tenants.HouseID=x.HouseID);
我为FALSE
值选择RentOverdue
,这似乎与您的逻辑一致。
答案 1 :(得分:2)
INSERT INTO
需要在表名后面必须插入给定数据的列的名称。显然,CURRENT_DATE
和NULL
不是列的名称。