Get last generated id in MySql [duplicate] - php

This question already has answers here:
Get current AUTO_INCREMENT value for any table
(9 answers)
Closed 6 years ago.
I have an auto-increment column 'id' . Now I want to echo the last generated id.
I have tried these queries -
SELECT MAX(id) FROM table_name
SELECT id from table_name ORDER BY id DESC LIMIT 1
SELECT LAST_INSERT_ID();
Above two queries returning the Max Id which is actually the last id generated.
But the problem is suppose if I delete a row where id is 10, then again I insert a row. Then the id would be 11 in the last row, not 10. But these 2 queries are returning the value 9. But I want the value 10.
LAST_INSERT_ID() should be used after an insert query which I dont need to do.
How can I get the id value of 10 ?

SELECT `AUTO_INCREMENT`
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'DatabaseName'
AND TABLE_NAME = 'TableName';
This was taken from
Get current AUTO_INCREMENT value for any table

Related

Selecting most recent row from a table [duplicate]

This question already has answers here:
Select last row in MySQL
(11 answers)
Closed last year.
id
first_die
24
2
25
6
How do i return the most recent first_die in this set? I want to display the latest roll in my select query. I know returning the latest first_die will be based on id but I'm having difficulty doing this because it is a simple fix, but I'm looking at not so simple answers to the problem. Any help would be nice. Thank you.
Use:
select id,first_die
from test_tbl
order by id desc limit 1 ;
Demo:
Or:
select id,first_die
from test_tbl
where id = (select max(id) from test_tbl);
Demo:
If your most recent id is the maximum number then you would use SELECT MAX(id) query (sql query).
If you want to return the entire recordset or row use the code below
SELECT * FROM tablename WHERE id=(SELECT MAX(id) FROM tablename);

How delete same value in Mysql [duplicate]

This question already has answers here:
How to delete duplicates on a MySQL table?
(25 answers)
Closed 6 years ago.
I want to delete same value in topic field and keep first row of value.
such as
no topic
1 1234
2 1234
3 1234
no = autoincrement
output
no topic
1 1234
This my code
$sql ="DELETE FROM data
WHERE no IN (SELECT *
FROM (SELECT no FROM data
GROUP BY topic HAVING (COUNT(*) > 1)
) AS A
)";
This code delete first value but I want to delete all same value and keep first value like example.
try this
DELETE FROM data
WHERE no NOT IN (SELECT no FROM
(SELECT MIN(no) as no,topic FROM data
GROUP BY topic
)NotDelete
);
sqlfiddle

How to get last inserted id from table MySQL [duplicate]

This question already has answers here:
How do I get the last inserted ID of a MySQL table in PHP?
(16 answers)
Last inserted id from specific table
(8 answers)
Closed 5 years ago.
I am running 1 script in php for that I need last inserted id in subscription table. By using that id I want to make notification note for that subscription.
I used:
SELECT LAST_INSERT_ID() FROM subscription
I am getting 0 instead of real last inserted value.
If you use php to connect to mysql you can use mysql_insert_id() to point to last inserted id.
Like this :
mysql_query("INSERT INTO mytable (1, 2, 3, 'blah')");
$last_id = mysql_insert_id();
See this : mysql_insert_id()
LAST_INSERT_ID() returns the last id from a previous insert statement. If you want the most recently inserted record and are using Auto Increment Prime keys, you can use the code below:
SELECT MAX( id ) FROM subscription;
If you need to know what the NEXT id will be, you can get this from INFORMATION_SCHEMA
SELECT AUTO_INCREMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'test'
mysql_insert_id
This question has already been answered many times: MySQL: LAST_INSERT_ID() returns 0
You are using that function out of context. It will only work if you inserted a row immediately prior thusly:
INSERT INTO 'subscription' (name) VALUES ('John Smith');
SELECT LAST_INSERT_ID() FROM subscription
You can however select the row with the highest id, which logically would be the most recently added...
SELECT MAX( id ) FROM subscription;
The standard approach however is to simply call mysqli_insert_id or mysql_insert_id (depending on whether you are using the mysqli or mysql PHP library. I should add that the mysql library is very inadvisable to use since it is almost completely deprecated). Here's what the whole thing would ideally look like:
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
$mysqli->query("INSERT INTO 'subscription' (name) VALUES ('John Smith');");
printf ("New Record has id %d.\n", $mysqli->insert_id);
//Output is something like: New Record has id 999
If however you didn't insert a subscription in the same script as collecting the most recent row ID, use the 'select max' approach. This seems unlikely given that you mentioned '1 script'
Also, if your ID's are non-consecutive, or you do not have an ID field, or you have row ID's higher than the one you just added you should probably consider a 'date_added' column to determine which one was really the latest. These scenarios are rather unlikely however.
this is the better approach 2 and 3 works but MAX(id) take more time to execute.
SELECT AUTO_INCREMENT FROM information_schema.TABLES WHERE TABLE_SCHEMA = 'dbName' AND TABLE_NAME = 'tableName';
SELECT tableName.id FROM tableName ORDER BY tableName.id DESC LIMIT 0,1;
SELECT MAX( id ) FROM tableName;
This will always give you the maximum id, which says the biggest number is the last inserted one
SELECT MAX(id) as MaximumID FROM subscription;
$last_id=mysql_query("SELECT id FROM `table_name` ORDER BY id DESC LIMIT 0 , 1" );
$row=mysql_fetch_assoc($last_id);
echo $row['id'];
Replace id by your id field name in database and table_name by your table name.

Selecting the most recently added record to the database in Mysql [duplicate]

This question already has answers here:
PostgreSQL, SELECT from max id
(5 answers)
Closed 4 years ago.
I have a table in database which has a primary key called id. Now i want to display the most recently added 2 records into the table.
Assuming id is some sort of auto-incrementing integer value then the following will work
SELECT * FROM table ORDER BY id DESC LIMIT 2;
If you want just the last inserted record id (again assuming the insert generates an autoincrement id) there is also LAST_INSERT_ID. But beware this is global and will return last inserted id database-wide so it is not often used in SELECTS but rather as an OUT paramer in a routine to return the id of the just-inserted row.
SELECT LAST_INSERT_ID();
This wil definitely work,
SELECT * FROM table_name ORDER BY id DESC,LIMIT 2.
Try,
SELECT * FROM `table`
ORDER BY id
DESC
LIMIT 0, 2;

how to select last autoincrement id using php and mysql [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
php/MySQL insert row then get 'id'
I have an inset command such as
insert (name) values($name);
I have id column as autoincrement. How can I get the id of the inserted record after inserting.
insert (name) values($name);
SET #lastid = LAST_INSERT_ID();
select blah_blah from table where id = #lastid;
To get that back into php, you do a normal mysql select on it like this:
select #lastid;
For mysql you can use mysql_insert_id to get the id of the last inserted row.

Categories