如果基于mysql中的列值存在alreay更新查询?

时间:2015-10-31 12:12:52

标签: php mysql

如果在mysql中找到重复的列值,如何更新查询

INSERT INTO receipt ( amount, bill_no) VALUES('100','1') 
ON DUPLICATE  bill_no='1' UPDATE amount='100'

我的实际查询是

insert into ig_receipt_payment 
    ( party, local_party_bill, date, amount, mode, description, options, type, job, bill_no, production_code ) 
values 
( '$_POST[worker]', '', '".date_merge("date")."', '$_POST[kooli_amount]', '', '$_POST[kooli_remark]', 'loading', 'credit', 'loading', '$_GET[id]', '' ) 
ON DUPLICATE   bill_no
update ig_receipt_payment set party = '$_POST[party]', local_party_bill = '$_POST[local_party_bill]', date = '".date_merge("date")."', amount = '$_POST[kooli_amount]',  description = '$_POST[kooli_remark]' 

请帮我解决这个问题。

1 个答案:

答案 0 :(得分:1)

首先,我会使用values简化您的查询,而不是重复常量并修复语法错误:

insert into ig_receipt_payment( party, local_party_bill, date, amount, mode, description, options, type, job, bill_no, production_code ) 
    values ( '$_POST[worker]', '', '".date_merge("date")."', '$_POST[kooli_amount]', '', '$_POST[kooli_remark]', 'loading', 'credit', 'loading', '$_GET[id]', '' ) 
ON DUPLICATE KEY UPDATE
       party = VALUES(party), local_party_bill = values(local_party_bill),
       date = VALUES(date), amount = VALUES(amount),
       description = VALUES(description);

然后,您需要一个唯一的索引/约束来定义重复项。我猜这是在列bill_no

create unique index unq_ig_receipt_payment_billno on ig_receipt_payment( bill_no)