1

I have table Product with fields : id (PK), title (UQ), price, status

I wrote query which should update row if table already has such title

INSERT INTO product (title, price, `status`) 
            VALUES("Black boots", 222.18, 1) 
                ON DUPLICATE KEY UPDATE price = 125.00 AND `status` = 2

That's just example for one product. However, after running this query I am getting message that query was successfuly executed, but price becomes = 0.00 (sometimes 1.00) and status does not change. What is my problem?

2 Answers 2

4

To update multiple values you need to use the comma operator:

INSERT INTO product (title, price, `status`) 
VALUES("Black boots", 222.18, 1) 
ON DUPLICATE KEY UPDATE price = 125.00, `status` = 2
Sign up to request clarification or add additional context in comments.

Comments

0

You can also use the argument Values. That allows you to INSERT / UPDATE more then one row

INSERT INTO product (title, price, `status`)
VALUES
 ("Black boots", 222.18, 1),
 ("Green boots", 111.22, 3)
ON DUPLICATE KEY UPDATE price = VALUES(price), `status` = VALUES(status);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.