在简单的insert语句中where子句附近的mysql错误

时间:2012-06-28 12:32:20

标签: mysql

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

它给出了以下mysql错误:

#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

知道我做错了吗?

1 个答案:

答案 0 :(得分:4)

INSERT语句用于插入新行,现在更新现有行,因此WHERE子句在INSERT中无效。您打算UPDATE

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

评论后编辑:

如果这是一个插入而不是更新,它就不能依赖orders_id = 15474之类的条件。如果要插入新行,则还需要插入这些值。

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