mysql - 带有ON DUPLICATE KEY UPDATE的查询中的条件插入行

时间:2014-05-13 05:40:01

标签: mysql

我有一个带有ON DUPLICATE KEY Update的复杂查询。我只想在没有“timestamp_dag”= 1420070400的行的情况下插入最后一个值,如果有一行有该条件我想什么都不做。

INSERT INTO data_prijzen_advertentie (
    `ID_advertentie`,`jaar`,`rijnr`,`status_prijs`,`datum_dag`,`timestamp_dag`,
    `prijs_maand`,`prijs_week`,`prijs_midweek`,`prijs_langweekend`,`prijs_weekend`,
    `prijs_dag`,`prijs_ochtend`,`prijs_middag`
)
VALUES
    (100,2014,1,1,'12-05-2014',1399852800,0,100,0,75,0,0,0,0),
    (100,2014,2,1,'23-05-2014',1400803200,0,75,0,101,0,0,0,0),
    (100,2014,3,1,'30-05-2014',1401408000,0,100,0,75,0,0,0,0),
    (100,2014,4,1,'01-01-2015',1420070400,0,0,0,0,0,0,0,0)
ON DUPLICATE KEY UPDATE
    status_prijs = VALUES(status_prijs), datum_dag = VALUES(datum_dag),
    timestamp_dag = VALUES(timestamp_dag), prijs_maand = VALUES(prijs_maand),
    prijs_week = VALUES(prijs_week), prijs_midweek = VALUES(prijs_midweek),
    prijs_langweekend = VALUES(prijs_langweekend), prijs_weekend = VALUES(prijs_weekend),
    prijs_dag = VALUES(prijs_dag), prijs_ochtend = VALUES(prijs_ochtend),
    prijs_middag = VALUES(prijs_middag);

2 个答案:

答案 0 :(得分:0)

如果您可以在创建查询字符串之前处理此问题,那么它是最好的。 但如果你不能,并且你的特殊价值是静态的,你可以尝试这样的事情: (尽管如此,我还没有尝试过它)

INSERT INTO data_prijzen_advertentie (
    `ID_advertentie`,`jaar`,`rijnr`,`status_prijs`,`datum_dag`,`timestamp_dag`,
    `prijs_maand`,`prijs_week`,`prijs_midweek`,`prijs_langweekend`,`prijs_weekend`,
    `prijs_dag`,`prijs_ochtend`,`prijs_middag`
)
VALUES
    (100,2014,1,1,'12-05-2014',1399852800,0,100,0,75,0,0,0,0),
    (100,2014,2,1,'23-05-2014',1400803200,0,75,0,101,0,0,0,0),
    (100,2014,3,1,'30-05-2014',1401408000,0,100,0,75,0,0,0,0),
    (100,2014,4,1,'01-01-2015',1420070400,0,0,0,0,0,0,0,0)
ON DUPLICATE KEY UPDATE
    status_prijs = IF(timestamp_dag<>1420070400, VALUES(status_prijs), status_prijs),
    datum_dag = IF(timestamp_dag<>1420070400, VALUES(datum_dag), datum_dag),
    timestamp_dag = IF(timestamp_dag<>1420070400, VALUES(timestamp_dag), timestamp_dag),
    prijs_maand = IF(timestamp_dag<>1420070400, VALUES(prijs_maand), prijs_maand),
    prijs_week = IF(timestamp_dag<>1420070400, VALUES(prijs_week), prijs_week),
    prijs_midweek = IF(timestamp_dag<>1420070400, VALUES(prijs_midweek), prijs_midweek),
    prijs_langweekend = IF(timestamp_dag<>1420070400, VALUES(prijs_langweekend), prijs_langweekend),
    prijs_weekend = IF(timestamp_dag<>1420070400, VALUES(prijs_weekend), prijs_weekend),
    prijs_dag = IF(timestamp_dag<>1420070400, VALUES(prijs_dag), prijs_dag),
    prijs_ochtend = IF(timestamp_dag<>1420070400, VALUES(prijs_ochtend), prijs_ochtend),
    prijs_middag = IF(timestamp_dag<>1420070400, VALUES(prijs_middag), prijs_middag);

答案 1 :(得分:0)

如果timestamp_dag与触发它的条件匹配,则只会运行ON DUPLICATE KEY UPDATE ...部分。来自MySQL docs

  

如果指定ON DUPLICATE KEY UPDATE,则插入一行   会导致UNIQUE索引或PRIMARY KEY中的重复值   执行旧行的更新。

因此假设您的表是以这种方式构建的,那么更新将会触发。

您可以使用语法as described here添加UNIQUE索引。

CREATE UNIQUE INDEX ind_unique_timestamp_dag ON data_prijzen_advertentie (timestamp_dag)