2012-03-30 102 views
9

Scenerio:MySQL如何在使用INSERT IGNORE INTO时检索LAST_INSERT_ID的ID?

  1. 我插入CSV文件到我的数据库。
  2. 我使用JDBC驱动程序来遍历文件和做我的刀片
  3. 一个name列成为unique所以当相同的值到达
  4. 我使用INSERT INTO IGNORE保持刀片不改表从当事件发生
  5. 我使用LAST_INSERT_ID()在插入的下一个表添加为FK突破迭代
  6. 我得到0时INSERT_IGNORE发生

我该如何解决这个问题? CSV文件的

Connection con = null; 

    try 
    { 
     Class.forName("com.mysql.jdbc.Driver"); 
     con = DriverManager.getConnection(URL, USERNAME, PASSWORD); 
     con.setAutoCommit(false); 

     Statement s1 = (Statement) con.createStatement(); 
     s1.executeUpdate("INSERT IGNORE INTO ORIGIN (ORI_NAME) VALUES(\"" 
      + d.getOriginName() + "\")"); 

     Statement s2 = (Statement) con.createStatement(); 
     s2.executeUpdate("INSERT IGNORE INTO STOCK (ORI_ID,S_TITLE) VALUES (" 
      + "(SELECT ORI_ID FROM ORIGIN WHERE ORI_ID =LAST_INSERT_ID()),\"" 
      + d.getStockTitle() + "\")"); 

    } 

     con.commit(); 

    } 
    catch (Exception e) 
    { 
     if (con != null) 
     try 
     { 
      con.rollback(); 
     } 
     catch (SQLException e1) 
     { 
      e1.printStackTrace(); 
     } 

     e.printStackTrace(); 
    } 
    finally 
    { 
     if (con != null) 
     try 
     { 
      con.close(); 
     } 
     catch (SQLException e) 
     { 
      e.printStackTrace(); 
     } 
    } 

例被插入到数据库

US, P1, M, 200, 344 
US, P1, L, 233, 344 
US, P2, M, 444, 556 
MX, Q4, L, 656, 777 

表:http://sqlfiddle.com/#!2/b5752

+0

Wha t是你想解决的问题?如果您正在执行“INSERT IGNORE”并忽略,则没有插入,因此没有该事务的Last_insert_id。如果您需要上一个插入ID,则需要在插入后立即捕获它。但对于最近的插入尝试,如果没有发生,last_insert_id将为0. – 2012-03-30 12:57:13

+0

@DMac那么当不想在现有列已经存在的情况下替换现有列时,如何插入数据库? – stackoverflow 2012-03-30 13:12:59

+0

@stackoverflow我已经发布了一个适合mysql的答案。 – 2012-05-12 21:43:31

回答

2

如果你想获得一个ID为后续的插入,你的代码必须测试LAST_INSERT_ID的0值,如果它得到它,请在表上做一个SELECT来为插入的FK找到正确的值。

在伪代码,这将是这样的:

$err = mysql("insert ignore into mytable values ($x, $y)") 
if($err){ 
    do something for an error 
} 
$id = mysql("select last_insert_id()"); 
if($id == 0){ 
    $id = mysql("select id from othertable where condition is true for the record I want to reference in my insert"); 
    if($id == 0){ 
     do something for error 
    } 
} 
mysql("insert into othertable VALUES ($id, other stuff)") 

你必须把这种对自己的应用程序和语言。

+0

继承人我的表结构http://sqlfiddle.com/#!2/b5752,我会在上面的编辑提供一个csv文件的例子 – stackoverflow 2012-03-30 13:13:31

5

您是否熟悉MySQL的ON DUPLICATE KEY UPDATE语法?

http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html

// Results are the same as doing an update. 
// Note the way the PK can be used to avoid an IGNORE. 
s1.executeUpdate("INSERT INTO ORIGIN (ORI_NAME) VALUES(\"" 
      + d.getOriginName() + "\" ON DUPLICATE KEY UPDATE ORI_ID=ORI_ID)"); 

一之实践例子这里涉及失败的电子邮件地址的网站维护:

http://blog.developpez.com/james-poulson/p10016/sql/ne-rien-modifier-en-cas-de-duplicate-key/

至于检索最后插入的ID是从MySQL采取以下.com以上链接:

Suppose that id is the AUTO_INCREMENT column. To make LAST_INSERT_ID() meaningful for updates, insert rows as follows: 

INSERT INTO table (a,b,c) VALUES (1,2,3) 
    ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id), c=3; 
+2

这应该是肯定的接受答案。完美的工作,并节省了很多其他必需的逻辑,例如在接受的答案中提供的逻辑。 – teh1 2013-03-29 22:42:50