How to get the last inserted ID of a table, even if the last x record(s) have been deleted?
x can be 1 and can be like 200
Assuming you're talking about auto incremented IDs:
SELECT AUTO_INCREMENT FROM INFORMATION_SCHEMA.TABLES WHERE table_name = 'Table'
This will give you the next increment id. To get the last inserted id just minus 1.
Following query will give you the last id present in table
select id from table_name order by id desc limit 1;
Updated:
Then you should create table which contains two columns like table_name,
last_id and you have to always update the last_id when you insert data into particular table. With this way you can fetch the Last id inserted to table.
Related
leave_request tablein my table emp_id column contains duplicate data 3 times. i want to select all the data from the table and the duplicate data to count as once. here i am providing the screenshot of my table structure
You can try below -
select max(id),emp_id, emp_name
from tablename
group by emp_id, emp_name
i want to fetch all record from MySQL database table using PHP without last added record
I have 4 Columns in the Table, ID(auto) & title and message.
You can use this, if as you say, ID is auto_increment
$sql = "SELECT * FROM table WHERE ID != (SELECT MAX(ID) FROM TABLE) ORDER BY ID";
This is selecting all the records from your table, ordered by id, where the id is not the biggest one.
I'm inserting records, primary key is a uniqueId created by PHP.
How to retrieve last inserted uniqueId from MySQL?
Tnx
If it is an auto_incremented value you would use mysql_insert_id() (or its mysqli sibling) immediately after doing an insert to get the ID assigned to it.
If you wanted to get a non-auto incremented value you would need to SELECT the row with the highest value and sort by that value in descending order to do the job (assuming it is numeric or alphanumeric and increments sequentially):
SELECT id FROM tablename ORDER id DESC LIMIT 1
If it is a random ID than you would need to sort by date:
SELECT id FROM tablename ORDER datecol DESC LIMIT 1
If you don't have any other method of sorting these records you can get the last row in the table but there is no guarantee that record is the newest and thus you have no accuracy in your results.
How can I SELECT the last row in a MySQL table?
I'm INSERTING data and I need to retrieve a column value from the previous row.
(I'm using PHP by the way.)
the table1 something like this
table1
******************
cate_id | task_id | start_date | end_date | line |
1 2 30/04/2012 26/06/2012 text
3 1 26/06/2012 27/06/2012 text
2 1 27/06/2012 01/01/9999 text
There'sNO an auto_incrementin that table.
And my case is to update the existing last row in table and then insert a new one.
You've edited question so, here's update
SELECT MAX(cate_id) AS last_cate_id FROM table;
or you can get next ID by:
SELECT MAX(cate_id)+1 AS next_cate_id FROM table;
Without transactions this is very vulnerable for inserting same cate_id!
If you cant use them, for example because of MyISAM, you could insert with select.
INSERT INTO table
(cate_id, task_id ..)
VALUES
( (SELECT MAX(cate_id)+1 AS next_cate_id FROM table), 1 )
if you don't have order by you wont be able to get the "LAST" value or the first, because the order will not be the same (necessarily), if you don't have auto increment how can you know which one is the first or the last?, if you are working with date or auto increment you will be able to get that, however, lets say that you have a order by 'column1' you can do something like:
select * from table1 order by `column1` desc limit 1
I need to get next id from table (auto_increment).
I could just use SELECT * from table ORDER BY id DESC LIMIT 1;
For example I get 50. But if we delete from table two items I will get 48 but correct one
will be 51. How get correct value even we something delete from table ?
You can only use SHOW TABLE STATUS LIKE 'tablename' to fetch the auto_increment value. A simpler solution might be: SELECT MAX(id) + 1 FROM table, but this is buggy if the last entry was deleted.
show table status like 'table_name'
next id value is in 'Auto_increment' field
SHOW TABLE STATUS LIKE 'table'
The value you want is in the Auto_increment field.
Be careful about concurrency though: by the time you get around to using this value, some other client could have inserted into the table and thus your value is out of date. It's usually best to try to not need this.
SELECT LAST_INSERT_ID() + 1;
gets the last ID used in an insert in an autoincrement column + 1
I see two solutions for the next ID:
1) Select bigger value of a column with max function. Example: select max( id ) from table;
2) Using the command SHOW STATUS LIKE and get the correct index of array. Take a look: http://dev.mysql.com/doc/refman/5.1/en/show-table-status.html
Seems to me you're creating a race condition here.
Why exactly can you not insert the row you want to insert and then use LAST_INSERT_ID() to find it's ID?