代码之家  ›  专栏  ›  技术社区  ›  JSmith

为第二次插入检索自动递增的第一次插入ID[重复]

  •  -1
  • JSmith  · 技术社区  · 7 年前

    我使用的是JDBC和google应用程序脚本。

    product_id 参考文献 id product ).

    这是我的桌子

    产品:

    身份证,姓名

    产品数量:

    标识,产品标识,数量

    下面是我想用伪代码实现的:

    function update(row){
      insertProductSQL = "INSERT INTO products (name) VALUES (" + row.name + ")";
      insertProductQuantity = "INSERT INTO products_quantity (product_id, quantity) VALUES (" + /*HERE IS THE PROBLEM*/ + ", " + row.quantity + ")"
      var conn = getConnection(); //JDBC connection object
      var stmt = conn.createStatement();
      stmt.executeQuery(insertProductSQL)
      stmt = conn.createStatement();
      stmt.executeQuery(insertProductQuantity);
      conn.close();
    }
    

    所以我面临的问题是,我不知道SQL或JDBC是否提供了一种检索自动递增值的简单方法 身份证件 创建于第一个 insert 用它来做我的第二件事 插入

    1 回复  |  直到 7 年前
        1
  •  1
  •   Barmar    7 年前

    使用 LAST_INSERT_ID() 功能。

      insertProductQuantity = "INSERT INTO products_quantity (product_id, quantity) VALUES (LAST_INSERT_ID(), " + row.quantity + ")"