在 MySQL 上,insertIntoA
您应该能够:
SELECT LAST_INSERT_ID()
...在您用于插入的同一连接上,假设它是identity
您要查找的列值。
编辑:如果您正在这样做并且它不起作用(根据您的评论),我会查看中间层以了解发生了什么。MySQL 很好:
mysql> create table A (id int(11) not null auto_increment, descr varchar(64), primary key (id));
Query OK, 0 rows affected (0.13 sec)
mysql> create table B (fk int(11) not null, descr varchar(64));
Query OK, 0 rows affected (0.06 sec)
mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into A (descr) values ('Testing 1 2 3');
Query OK, 1 row affected (0.00 sec)
mysql> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
| 1 |
+------------------+
1 row in set (0.03 sec)
mysql> insert into B (fk, descr) values (1, 'Test complete');
Query OK, 1 row affected (0.00 sec)
mysql> commit;
Query OK, 0 rows affected (0.03 sec)
mysql> select * from A;
+----+---------------+
| id | descr |
+----+---------------+
| 1 | Testing 1 2 3 |
+----+---------------+
1 row in set (0.02 sec)
mysql> select * from B;
+----+---------------+
| fk | descr |
+----+---------------+
| 1 | Test complete |
+----+---------------+
1 row in set (0.00 sec)
mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into A (descr) values ('Second test');
Query OK, 1 row affected (0.01 sec)
mysql> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
| 2 |
+------------------+
1 row in set (0.00 sec)
mysql> insert into B (fk, descr) values (2, 'Second test complete');
Query OK, 1 row affected (0.00 sec)
mysql> commit;
Query OK, 0 rows affected (0.08 sec)
mysql> select * from A;
+----+---------------+
| id | descr |
+----+---------------+
| 1 | Testing 1 2 3 |
| 2 | Second test |
+----+---------------+
2 rows in set (0.02 sec)
mysql> select * from B;
+----+----------------------+
| fk | descr |
+----+----------------------+
| 1 | Test complete |
| 2 | Second test complete |
+----+----------------------+
2 rows in set (0.00 sec)
mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into A (descr) values ('We''ll roll this one back.');
Query OK, 1 row affected (0.00 sec)
mysql> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
| 3 |
+------------------+
1 row in set (0.00 sec)
mysql> insert into B (fk, descr) values (3, 'Won''t see this one.');
Query OK, 1 row affected (0.00 sec)
mysql> select * from B;
+----+----------------------+
| fk | descr |
+----+----------------------+
| 1 | Test complete |
| 2 | Second test complete |
| 3 | Won't see this one. |
+----+----------------------+
3 rows in set (0.00 sec)
mysql> rollback;
Query OK, 0 rows affected (0.03 sec)
mysql> select * from A;
+----+---------------+
| id | descr |
+----+---------------+
| 1 | Testing 1 2 3 |
| 2 | Second test |
+----+---------------+
2 rows in set (0.00 sec)
mysql> select * from B;
+----+----------------------+
| fk | descr |
+----+----------------------+
| 1 | Test complete |
| 2 | Second test complete |
+----+----------------------+
2 rows in set (0.00 sec)