我试图执行以下查询,但它总是报告错误

时间:2017-02-15 05:23:38

标签: sql postgresql

模式

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错误

2 个答案:

答案 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_DATENULL不是列的名称。