问题
I'm using JavaDB and working with two tables in the same database.
I'm trying to update the PrevSales column in the "SalesResp" table with the values that are in the "SellDate" column of the "Vehicles" table when the Vehicle table's SellDate is before a the specified date. I'm matching the data between the two tables by using the "VIN" column that is present in both tables.
I continually get errors surrounding the inner join in my code, SQL state 42x01.
UPDATE SALESRESP JOIN VEHICLES ON (SALESRESP.VIN = VEHICLES.VIN)
SET SALESRESP.PrevSale = VEHICLES.SELLDATE WHERE
(((VEHICLES.SELLDATE)<'2013-09-24'));
I've tried various configurations, swapping the INNER JOIN and the SET placements, I've tried including a "FROM" clause. Nothing seems to work. Any advice?
EDIT: Exact Error Code:
Error code 30000, SQL state 42X01: Syntax error: Encountered "JOIN" at line 1, column 19.
回答1:
Use the following code and see, replace your date instead of sysdate.
UPDATE SALESRESP
SET SALESRESP.PrevSale = (SELECT VEHICLES.SELLDATE FROM VEHICLES
WHERE SALESRESP.VIN = VEHICLES.VIN AND
VEHICLES.SELLDATE <'2013-09-24');
回答2:
UPDATE S SET S.PrevSale = V.SELLDATE FROM SALESRESP S
inner join VEHICLES V on S.VIN = V.VIN AND V.SELLDATE < '2013-09-24'
回答3:
OK, let's try being more explicit:
UPDATE SALESRESP
SET SALESRESP.PrevSale = VEHICLES.SELLDATE
FROM VEHICLES
JOIN SALESREP ON (SALESRESP.VIN = VEHICLES.VIN)
WHERE (((VEHICLES.SELLDATE)<'2013-09-24'))
See if that will work.
来源:https://stackoverflow.com/questions/26218921/update-with-inner-join