1

I try to use mysql replace with following table.

CREATE TABLE price (
   id BIGINT UNSIGNED AUTO_INCREMENT, 
   ski_chalet_id BIGINT UNSIGNED NOT NULL, 
   month DATE NOT NULL, 
   d_1 DOUBLE(18, 2), 
   d_2 DOUBLE(18, 2), 
   d_3 DOUBLE(18, 2), 
   d_4 DOUBLE(18, 2), 
   d_5 DOUBLE(18, 2), 
   created_at DATETIME NOT NULL, 
   updated_at DATETIME NOT NULL, 
   UNIQUE INDEX fk_ski_chalet_price_ski_chalet_idx (ski_chalet_id, month), 
   INDEX ski_chalet_id_idx (ski_chalet_id), 
   PRIMARY KEY(id)
) DEFAULT CHARACTER SET utf8 ENGINE = InnoDB;

I want add a row if row is not exist and update if row is exist. So i used mysql REPLACE to do that.

REPLACE INTO  `price` SET `ski_chalet_id` = 43 and `month` = '2013-04-01' and `d_1` = 23

This query is successful. but add all value as 00 and 0000-00-00 or null.

But insert query is working.

INSERT INTO `price` (`ski_chalet_id`,`month`,`d_1`) VALUES ('34', '2013-04-01', '46');

I cant find whats the issue. Please help me.

4

3 回答 3

2

您必须在替换查询中使用“,”而不是“和”......

替换 为priceSET ski_chalet_id= 43 , month= '2013-04-01' , d_1= 23

注意:如果您希望您不必寻找其他功能,则 REPLACE 功能将起作用。

于 2013-04-18T06:20:25.620 回答
0

不要SETREPLACE. for 的语法与 forREPLACE相同INSERT

REPLACE INTO `price` (`ski_chalet_id`,`month`,`d_1`) VALUES ('43', '2013-04-01', '23');
于 2013-04-18T06:15:06.033 回答
0

SET要使用in REPLACE(和UPDATE/ )指定多个列INSERT,必须用逗号分隔它们。您的查询相当于:

SET `ski_chalet_id` = (some boolean expression)

改写为

SET `ski_chalet_id` = 43, `month` = '2013-04-01', `d_1` = 23
于 2013-04-18T06:16:43.647 回答