How to update SQL row based on two or more duplicate coluns

你。 提交于 2021-02-11 18:16:14

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!