Is there a way to get last inserted id of a NON - auto incremented column in MySQL?

前端 未结 7 1293
别跟我提以往
别跟我提以往 2020-12-17 14:24

I know how LAST_INSERT_ID() works for auto incremented columns, but I cannot find a way to get the last id I inserted for a non auto incremented column.

Is there a

相关标签:
7条回答
  • 2020-12-17 14:52

    I assume that you need the ID to find your just inserted row, rather to find the last inserted row. In a web application, you can never be sure that the last inserted row is the one you have just created.

    You could use a GUID as id in this case. A GUID is usually stored as a string of length 36 or as a 16byte blob. The GUID can be created before inserting the row, and then can be stored while inserting the row.

    Since the id is not auto incremented as you stated, you have to generate it anyway before inserting the row. The safest way to do this is to create a GUID which should be unique enough. Otherwise you would have to determine the last unused ID, what can be tricky and risky.

    0 讨论(0)
  • 2020-12-17 14:55

    If you want to get a custom last_inserted ID, you must implement a procedure that will make the insert statment on your DB. At the end, just print the ID and use the PHP (if PHP is your main script) sender to return the generated row.

    EXAMPLE:

    DROP PROCEDURE IF EXISTS insert_row;
    DELIMITER $$
    
    CREATE PROCEDURE insert_row(IN _row_id VARCHAR(255), IN _description VARCHAR(255))
    BEGIN
        SET @last_inserted_id = _row_id;
        SET @sql = CONCAT("INSERT INTO test VALUES ('", _row_id, "','",_description,"')");
        PREPARE stmt FROM @sql;
        EXECUTE stmt;
        DEALLOCATE PREPARE stmt;
        SELECT @last_inserted_id AS LAST_INSERT_ID;
    END;
    $$
    
    DELIMITER ;
    #
    #
    #
    #------- HOW TO USE IT ? ---------------
    CALL insert_row('Test001','the first test line');
    
    0 讨论(0)
  • 2020-12-17 15:01

    you can easily do that using the same LAST_INSERT_ID().

    INSERT INTO thetable (id, value)
    VALUES (LAST_INSERT_ID(126), 'some data');
    
    SELECT LAST_INSERT_ID();  -- returns 126
    
    0 讨论(0)
  • 2020-12-17 15:03

    There's no way with mysql. But you can to do it programmatically. Without an auto-incrementing ID column there's no way for the database to know which records were inserted last.

    One way to do is use such as a column containing timestamp or datetime values. and get id of latest value of tmestamp to get last inserted record

    0 讨论(0)
  • 2020-12-17 15:05

    This worked for me in XAMPP

    $qry = $con->query("INSERT INTO test_table(tbl_id, txt) VALUES(last_insert_id('15'), 'test value')");
    print_r($con->insert_id);
    
    0 讨论(0)
  • 2020-12-17 15:12

    No.

    There is no inherent ordering of relations, no "last-inserted record". This is why the AUTO_INCREMENT field exists, after all.

    You'd have to look in logs or cache the value yourself inside your application.

    0 讨论(0)
提交回复
热议问题