insert into tab values(NULL,'my_name');
SELECT LAST_INSERT_ID();

解决方案 »

  1.   

    to:  zhouzhq009 ()  你的表建的就不对,对于自增子段不能为空。怎么能插入null呢?
    建议:
    create table tab(id int not null unsigned AUTO_INCREMENT primary key,name varchar(20));
      

  2.   

    to shuixin13(犬犬(心帆)) 我用SELECT LAST_INSERT_ID();查,怎么都是"0"???怎么回事??
      

  3.   

    呵呵
    不可能吧,
    下面是我的 mysql.exe 屏幕显示>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 1 to server version: 4.0.11-gamma-max-nt-logType 'help;' or '\h' for help. Type '\c' to clear the buffer.mysql> use test
    Database changed
    mysql> create table tab(id int unsigned AUTO_INCREMENT,name varchar(20));
    ERROR 1075: 表结构定义出错; 只能定义一个自增列并将该列建立索引
    mysql> create table tab(id int unsigned AUTO_INCREMENT,name varchar(20),primary
    key(id));
    Query OK, 0 rows affected (0.02 sec)mysql> insert into tab values(NULL,'my_name');
    Query OK, 1 row affected (0.04 sec)mysql> SELECT LAST_INSERT_ID();
    +------------------+
    | last_insert_id() |
    +------------------+
    |                1 |
    +------------------+
    1 row in set (0.00 sec)mysql> insert into tab values(NULL,'my_name');
    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>
    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    你通过什么访问 MySQL 数据库的呀???
      

  4.   

    你如果用的是ado可能會用問題.
      

  5.   

    我是這樣來取的
    set rs=conn.Execute("show table status like 'tablename'")
    nextautoid=rs("Auto_increment")
    這是執行insert前的下一個自動ID號
      

  6.   

    to shuixin13(犬犬(心帆)) :用DOS的MYSQL没有问题,用MYSQL的mysqlgui-win32-static-1.7.5-2就看不出来?呵呵
      

  7.   

    呵呵
    不怎么用 mysqlgui-win32-static执行这个函数时
    你必须保证在同一个会话ID下
    因为 LAST_INSERT_ID() 只对当前连接有效