MySQL - Select the last inserted row easiest way

后端 未结 9 1883
误落风尘
误落风尘 2020-11-30 09:56

I simply need to select the last entered row specified by condition, e.g:

SELECT ID from bugs WHERE user=Me

I need to return only the very

相关标签:
9条回答
  • 2020-11-30 10:29

    You can use ORDER BY ID DESC, but it's WAY faster if you go that way:

    SELECT * FROM bugs WHERE ID = (SELECT MAX(ID) FROM bugs WHERE user = 'me')
    

    In case that you have a huge table, it could make a significant difference.

    EDIT

    You can even set a variable in case you need it more than once (or if you think it is easier to read).

    SELECT @bug_id := MAX(ID) FROM bugs WHERE user = 'me';
    SELECT * FROM bugs WHERE ID = @bug_id;
    
    0 讨论(0)
  • 2020-11-30 10:29

    Just after running mysql query from php

    get it by

    $lastid=mysql_insert_id();
    

    this give you the alst auto increment id value

    0 讨论(0)
  • 2020-11-30 10:30

    SELECT ID from bugs WHERE user=Me ORDER BY CREATED_STAMP DESC; BY CREATED_STAMP DESC fetches those data at index first which last created.

    I hope it will resolve your problem

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