mysql - Insert into a table with auto_increment, but return other column as LAST_INSERT_ID -


i have table auto incrementing primary key. want insert row it, return else last_insert_id. reading manual, thought possible do:

create table test (     id bigint auto_increment primary key,     number bigint );  insert test (number) values (last_insert_id(50));  select last_insert_id(); 

i want return 50, returns 1 instead.

the documentation states that:

the return value of mysql_insert_id() can simplified following sequence:

  1. if there auto_increment column, , automatically generated value inserted, return first such value.

  2. if last_insert_id(expr) occurred in statement, return expr, if there auto_increment column in affected table.

  3. the return value varies depending on statement used. when called after insert statement:

    • if there auto_increment column in table, , there explicit values column inserted table, return last of explicit values.

    when called after insert ... on duplicate key update statement:

    • if there auto_increment column in table , there explicit inserted values or updated values, return last of inserted or updated values.

if i'm interpreting point 2. correctly, query should work , should return 50, not 1. looks point 1. precedes point 2.

is there other way achieve without having run multiple queries?

last_insert_id() - value of autoincrement column last insert
(automatically generated value inserted auto_increment column),
it this page.

it mean have use function this:

create table test (     id bigint auto_increment primary key,     number bigint );  insert test (number) values (200);  select last_insert_id(); +------------------+ | last_insert_id() | +------------------+ |                1 | +------------------+  insert test (number) values (100);  select last_insert_id(); +------------------+ | last_insert_id() | +------------------+ |                2 | +------------------+ 

but:

insert test values (50, 50);  select last_insert_id(); +------------------+ | last_insert_id() | +------------------+ |                2 | +------------------+  select * test; +----+--------+ | id | number | +----+--------+ |  1 |    200 | |  2 |    100 | | 50 |     50 | +----+--------+ 

it because id 50 not generated mysql...


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -