问题
I have the following SQL table. I want to be able to insert a new row (if it doesn't exist) or update a value in an existing row (in this case transaction
based TWO columns which are (ds, item)
.
ID ds item transactions
1 2020-02-25 Baba Ghanous 1.0
2 2020-02-25 Shawerma 1.0
3 2020-02-25 Steak 1.0
4 2020-02-25 fish n chips 1.0
5 2020-02-25 Besara 1.0
Initially, i started with the following query:
INSERT INTO forecast_sales (ds, item, transactions) VALUES('2020-02-25', 'Shawerma', 1.0) ON DUPLICATE (ds, item) UPDATE transaction=2.0
However, I get a Syntax error because Duplicate is used with the associated key of the table according to the documentation. My question is, is there a query that can allow me to produce the desired result?
回答1:
Dates need to be quoted. The error message shows exactly where the syntax error starts.
Documentation for INSERT ON DUPLICATE shows DUPLICATE (ds, item)
isn't part of the syntax allowed.
来源:https://stackoverflow.com/questions/60200157/how-to-update-sql-row-based-on-two-or-more-duplicate-coluns