1

I need to update a table by some condition, not with primary key.

I am trying to do something like following

update p_price set price = :price where p_id = :pid and c_id = :cid if there is no any row with where p_id = :pid and c_id = :cid then insert into p_price (p_id, c_id, price) value (1,2,3)

is that possible with sql command?

note: there are no primary key so I can not use ON DUPLICATE KEY.

4

1 回答 1

4

实际上它不需要有主键。它只需要至少唯一的约束。添加此UNIQUE约束

ALTER TABLE p_price ADD CONSTRAINT tb_unique UNIQUE(p_id , c_id);

并执行此语句,

insert into p_price (p_id, c_id, price) 
value (1,2,3)
ON DUPLICATE KEY 
UPDATE price = :price

只需更改您首先需要的值。

于 2013-04-22T12:17:19.723 回答