Bluemagica
Bluemagica

Reputation: 5158

mysql error near where clause in simple insert statement

INSERT INTO orders_total SET `title`='Discount Coupon:', `text` = '-$40.00', `value` = '40.00' WHERE `orders_id` = '15474' AND `class`='ot_coupon

It gives the following mysql error:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE `orders_id` = '15474' AND `class`='ot_coupon'' at line 1

Any idea what I am doing wrong?

Upvotes: 0

Views: 138

Answers (1)

Michael Berkowski
Michael Berkowski

Reputation: 270609

INSERT statements are meant to insert new rows, now update existing ones, and a WHERE clause is therefore invalid in an INSERT. You intended to UPDATE.

UPDATE orders_total
SET 
  `title`='Discount Coupon:', 
  `text` = '-$40.00', 
  `value` = '40.00'
WHERE 
  `orders_id` = '15474' 
  AND `class`='ot_coupon'

Edit after comments:

If this was intended to be an insert rather than an update, it cannot have dependencies on conditions like orders_id = 15474. If you are inserting a new row, you need to insert those values as well.

INSERT INTO orders_total (`orders_id`, `class`, `title`, `text`, `value`) VALUES (15474, 'ot_coupon', 'Discount Coupon:', '-$40.00', '40.00');

Upvotes: 4

Related Questions