如果value不存在,mysql只插入行

时间:2014-05-12 19:26:50

标签: mysql

一个复杂的mysql问题!我只想插入最后一个值(零值),如果没有其他行的值为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);

1 个答案:

答案 0 :(得分:0)

执行此操作的一种方法是使用SELECT代替VALUES子句。使用SELECT语句返回要插入的行。例如:

  SELECT 100 AS a,2014 AS b,1 AS c,1 AS d,'12-05-2014' AS e
         ,1399852800 AS timestamp_dag
         ,0 AS g,100 AS h,0 AS i,75 AS j,0 AS k,0 AS l,0 AS m,0 AS n
  UNION ALL
  SELECT 100,2014,2,1,'23-05-2014',1400803200,0,75,0,101,0,0,0,0
  UNION ALL
  SELECT 100,2014,3,1,'30-05-2014',1401408000,0,100,0,75,0,0,0,0
  UNION ALL
  SELECT 100,2014,4,1,'01-01-2015',1420070400,0,0,0,0,0,0,0,0

使用SELECT,还可以检查表中已存在的行。您可以将上面的查询用作内联视图(即将该查询包装在parens中并为派生表分配别名)并对其编写SELECT(就像它是一个表),并使用{{1谓词或反连接模式来抑制行返回"匹配"行已存在于目标表中。例如:

NOT EXISTS

(注意:LEFT JOIN操作返回派生表 SELECT s.* FROM ( SELECT 100 AS a,2014 AS b,1 AS c,1 AS d,'12-05-2014' AS e ,1399852800 AS timestamp_dag ,0 AS g,100 AS h,0 AS i,75 AS j,0 AS k,0 AS l,0 AS m,0 AS n UNION ALL SELECT 100,2014,2,1,'23-05-2014',1400803200,0,75,0,101,0,0,0,0 UNION ALL SELECT 100,2014,3,1,'30-05-2014',1401408000,0,100,0,75,0,0,0,0 UNION ALL SELECT 100,2014,4,1,'01-01-2015',1420070400,0,0,0,0,0,0,0,0 ) s LEFT JOIN data_prijzen_advertentie t ON t.timestamp_dag = s.timestamp_dag WHERE t.timestamp_dag IS NULL 中的所有行,以及来自s的匹配行;"技巧"是使用WHERE子句消除了找到匹配项的所有行,因此我们留下t中没有s中匹配行的行。使用NOT EXISTS谓词可以实现同样的效果相关子查询。)

可能需要将此查询包装在parens中并将其作为内联视图引用(因此它又是一个派生表)以避免变异表发布/错误。例如:

t

一旦有查询工作返回所需的行(不包括目标表中已存在匹配行的行),您可以用 SELECT r.* FROM ( SELECT s.* FROM ( SELECT 100 AS a,2014 AS b,1 AS c,1 AS d,'12-05-2014' AS e ,1399852800 AS timestamp_dag ,0 AS g,100 AS h,0 AS i,75 AS j,0 AS k,0 AS l,0 AS m,0 AS n UNION ALL SELECT 100,2014,2,1,'23-05-2014',1400803200,0,75,0,101,0,0,0,0 UNION ALL SELECT 100,2014,3,1,'30-05-2014',1401408000,0,100,0,75,0,0,0,0 UNION ALL SELECT 100,2014,4,1,'01-01-2015',1420070400,0,0,0,0,0,0,0,0 ) s LEFT JOIN data_prijzen_advertentie t ON t.timestamp_dag = s.timestamp_dag WHERE t.timestamp_dag IS NULL ) r 语句中的VALUES子句替换查询。

注意:这仅在查询运行时检查表中是否存在行。这不会检查查询返回的结果集,以查看是否有两个(或更多)行具有INSERT值。