Assume your table has a auto_increment field,then you'll get the sql statement here.insert into a(field1,field2) values ('标题','内容'); insert into b(a_id,field1) values (last_insert_id(),'类别');
如果A表中的ID是INCREMENT ,则可以用LAST_INSERT_ID()获取最后的ID值。以下摘自 MySQL 5.1 Reference Manual
LAST_INSERT_ID(), LAST_INSERT_ID(expr) For MySQL 5.1.12 and later, LAST_INSERT_ID() (no arguments) returns the first automatically generated value successfully inserted for an AUTO_INCREMENT column as a result of the most recently executed INSERT statement. The value of LAST_INSERT_ID() remains unchanged if no rows are successfully inserted. For example, after inserting a row that generates an AUTO_INCREMENT value, you can get the value like this: mysql> SELECT LAST_INSERT_ID(); -> 195. [align=center]====思想重于技巧==== [/align] . 贴子分数<20:对自已的问题不予重视。 贴子大量未结:对别人的回答不予尊重。 .
insert into b(a_id,field1) values (last_insert_id(),'类别');
LAST_INSERT_ID(), LAST_INSERT_ID(expr) For MySQL 5.1.12 and later, LAST_INSERT_ID() (no arguments) returns the first automatically generated value successfully inserted for an AUTO_INCREMENT column as a result of the most recently executed INSERT statement. The value of LAST_INSERT_ID() remains unchanged if no rows are successfully inserted. For example, after inserting a row that generates an AUTO_INCREMENT value, you can get the value like this: mysql> SELECT LAST_INSERT_ID();
-> 195.
[align=center]==== 思想重于技巧 ====
[/align]
.
贴子分数<20:对自已的问题不予重视。
贴子大量未结:对别人的回答不予尊重。
.
如果我的A表的自增字段名是sb_id
那
insert into a(field1,field2) values ('标题','内容');
insert into b(a_id,field1) values (last_insert_id(),'类别'); 得改成什么?
这样就应该可以了。.
[align=center]==== 思想重于技巧 ====
[/align]
.
贴子分数<20:对自已的问题不予重视。
贴子大量未结:对别人的回答不予尊重。
.
不行啊,查看数据库,a_id的值全部是0,
不好意思,以前一直是用ASP+MSSQL的,第一次接触PHP,
贴子肯定会结的
[align=center]==== 思想重于技巧 ====
[/align]
.
贴子分数<20:对自已的问题不予重视。
贴子大量未结:对别人的回答不予尊重。
.