一个表第一列是ID号,自动垒加。
插入数据后如果得到ID号是多少?

解决方案 »

  1.   

    select LAST_INSERT_ID();
      

  2.   

    LAST_INSERT_ID() 或者@@identitymysql> create table t1 (
        ->  id int AUTO_INCREMENT PRIMARY KEY,
        ->  c1 int
        -> );
    Query OK, 0 rows affected (0.11 sec)mysql> insert into t1 values (null,11);
    Query OK, 1 row affected (0.06 sec)mysql> select * from t1;
    +----+------+
    | id | c1   |
    +----+------+
    |  1 |   11 |
    +----+------+
    1 row in set (0.00 sec)mysql>
    mysql> insert into t1 values (null,22);
    Query OK, 1 row affected (0.06 sec)mysql> select LAST_INSERT_ID();
    +------------------+
    | LAST_INSERT_ID() |
    +------------------+
    |                2 |
    +------------------+
    1 row in set (0.00 sec)mysql> select @@identity;
    +------------+
    | @@identity |
    +------------+
    |          2 |
    +------------+
    1 row in set (0.06 sec)mysql> select * from t1;
    +----+------+
    | id | c1   |
    +----+------+
    |  1 |   11 |
    |  2 |   22 |
    +----+------+
    2 rows in set (0.00 sec)mysql>
      

  3.   

    我是用C api
    mysql.h中好像没这个函数发现下面这个函数
    my_ulonglong STDCALL mysql_insert_id(MYSQL *mysql);
    不知道是不是这个
      

  4.   

    C API中也一样。 手册中都有说明。
    mysql_insert_id()my_ulonglong mysql_insert_id(MYSQL *mysql) Returns the value generated for an AUTO_INCREMENT column by the previous INSERT or UPDATE statement.