[<<][sql][>>][..]
Sat Mar 3 09:15:21 EST 2012

Playing with INSERT

   DROP TABLE if exists foo;
   CREATE TABLE foo(
          id   MEDIUMINT NOT NULL AUTO_INCREMENT,
          val  TEXT,
          PRIMARY KEY (id)
          );

   INSERT IGNORE foo (val) VALUES ("lalala");


From [1]: The logic is that the SELECT statement generates a single
row of data with the required values, but only when the values are not
already found.  That wasn't immediately useful.  Dicking around a bit
I come to this:

   INSERT INTO foo(val)
     (SELECT (val) from foo 
      WHERE NOT EXISTS (SELECT * FROM foo WHERE val = "lalala"));

   INSERT INTO foo(val)
     (SELECT "lalala" from DUAL
      WHERE NOT EXISTS (SELECT * FROM foo WHERE val = "lalala"));

Turning this into a procedure doesn't work..  I asked Yvonne, she
probably knows.



[1] http://stackoverflow.com/questions/913841/mysql-conditional-insert




[Reply][About]
[<<][sql][>>][..]