How to receive the last row id in the database without generating a new row or updating the row.
Just to read the last id?
I have the function mysql_insert_id and need something like this, but without generating a new row.
SELECT MAX(id) AS latest_id
FROM table_name
If you instead want the comming (and not yet existing) id without inserting a row, see my previous answer.
You need to use max() function like
select max(yourcol) from yourtable
You can try lot of queries for that :-
select * from TABLE_NAME order by ID desc limit 1;
"or"
SELECT *
FROM TABLE
WHERE ID = (SELECT MAX(ID) FROM TABLE);
Related
I want to select latest row from approver table based on a request id.
Also i need to join request table and approver table based on request id
Tried latest() method but it will return only last record. But i want to retrieve last last row of each request id.
Tried group by and distinct but not worked. Could you please suggest a better way
maybe this would be work, use orderBy('column_name',desc)->first()
there are two way to do this :
code a loop and get rows by a query for each row.
try this code :
\DB::table('my_table')->selectRaw('*,max(id) as last')->groupBy('user_id')->get();
Use this query:
$last_approver = Approver::orderBy('id', 'DESC')->first();
select * from approver where request_id = <your request id> order by <some column like created_at> limit 1
I want to show highest record id from mysql database on live. I am using following code and it's working on localhost but not on live site.
<?php
$q ="SELECT LAST_INSERT_ID()";
$result = mysqli_query($q);
$data = mysqli_fetch_array($result);
echo $data[0];
?>
As Jon mentioned you need to add MAX(). So did you setup your live database exactly the same as the localhost one? Maybe tell us the error?
(Can't comment yet, sorry)
use sub select query if id is unique.
select row
from table
where id=(
select max(id) from table
)
if id is not unique then use this.
select row from table ORDER BY id DESC LIMIT 1
you can use id column if you are using as PK and auto increment.
Select * from TABLE order BY id DECS LIMIT 1
if you are talking about concurrent queries then i have done something different to tackle this situation.
insert a timestamp in a new column and then fetch the same
please check the below code
$token= data();
insert into TABLE ('val1', 'val2', $token);
and then
you can user $token to get id of last inserted row by something like this
Select * from TABLE where token = $token
I'm currently using:
SELECT MAX(id) FROM table
To discover the current id of a certain table, but i heard this can bring bad results. What is the proper way of doing that? Please, notice that i'm not INSERTING or DELETING anything before that query. I just want to know the current ID, without prior INSERT or DELETE.
Perform the following SQL:
SHOW TABLE STATUS LIKE 'TABLENAME'
Then check field AUTO_INCREMENT
You can use the following query:
SELECT id FROM table ORDER BY id DESC LIMIT 1;
I am trying to update fields in my DB, but got stuck with such a simple problem: I want to update just one row in the table with the biggest id number. I would do something like that:
UPDATE table SET name='test_name' WHERE id = max(id)
Unfortunatelly it doesnt work. Any ideas?
Table Structure
id | name
---|------
1 | ghost
2 | fox
3 | ghost
I want to update only last row because ID number is the greatest one.
The use of MAX() is not possible at this position. But you can do this:
UPDATE table SET name='test_name' ORDER BY id DESC LIMIT 1;
For multiple table, as #Euthyphro question, use table.column.
The error indicates that column id is ambiguous.
Example :
UPDATE table1 as t1
LEFT JOIN table2 as t2
ON t2.id = t1.colref_t2
SET t1.name = nameref_t2
ORDER BY t1.id DESC
LIMIT 1
UPDATE table SET name='test_name' WHERE id = (SELECT max(id) FROM table)
This query will return an error as you can not do a SELECT subquery from the same table you're updating.
Try using this:
UPDATE table SET name='test_name' WHERE id = (
SELECT uid FROM (
SELECT MAX(id) FROM table AS t
) AS tmp
)
This creates a temporary table, which allows using same table for UPDATE and SELECT, but at the cost of performance.
I think iblue's method is probably your best bet; but another solution might be to set the result as a variable, then use that variable in your UPDATE statement.
SET #max = (SELECT max(`id`) FROM `table`);
UPDATE `table` SET `name` = "FOO" WHERE `id` = #max;
This could come in handy if you're expecting to be running multiple queries with the same ID, but its not really ideal to run two queries if you're only performing one update operation.
UPDATE table_NAME
SET COLUMN_NAME='COLUMN_VALUE'
ORDER BY ID
DESC LIMIT 1;
Because you can't use SELECT IN DELETE OR UPDATE CLAUSE.ORDER BY ID DESC LIMIT 1. This gives you ID's which have maximum value MAX(ID) like you tried to do. But MAX(ID) will not work.
Old Question, but for anyone coming across this you might also be able to do this:
UPDATE
`table_name` a
JOIN (SELECT MAX(`id`) AS `maxid` FROM `table_name`) b ON (b.`maxid` = a.`id`)
SET a.`name` = 'test_name';
We can update the record using max() function and maybe it will help for you.
UPDATE MainTable
SET [Date] = GETDATE()
where [ID] = (SELECT MAX([ID]) FROM MainTable)
It will work the perfect for me.
I have to update a table with consecutive numbers.
This is how i do.
UPDATE pos_facturaciondian fdu
SET fdu.idfacturacompra = '".$resultado["afectados"]."',
fdu.fechacreacion = '".$fechacreacion."'
WHERE idfacturaciondian =
(
SELECT min(idfacturaciondian) FROM
(
SELECT *
FROM pos_facturaciondian fds
WHERE fds.idfacturacompra = ''
ORDER BY fds.idfacturaciondian
) as idfacturaciondian
)
Using PHP I tend to do run a mysqli_num_rows then put the result into a variable, then do an UPDATE statement saying where ID = the newly created variable. Some people have posted there is no need to use LIMIT 1 on the end however I like to do this as it doesn't cause any trivial delay but could prevent any unforeseen actions from being taken.
If you have only just inserted the row you can use PHP's mysqli_insert_id function to return this id automatically to you without needing to run the mysqli_num_rows query.
Select the max id first, then update.
UPDATE table SET name='test_name' WHERE id = (SELECT max(id) FROM table)
How can I get the latest ID in a table?
If you mean the latest generated ID from an insert statement with an auto-increment column, then mysql_insert_id() should help ya out
SELECT max(id) FROM table
SELECT id FROM table ORDER BY id DESC LIMIT 1 should work as well
IF you've just inserted into a table with auto_increment you can run right after your query.
SELECT last_insert_id();
Otherwise the max(id) FROM table
If the table has an auto_increment column defined - you can check by looking for "auto_increment" in the output from DESC your_table, use:
mysql_insert_id
Otherwise, you have these options:
SELECT MAX(id) FROM your_table
SELECT id FROM your_table ORDER BY id LIMIT 1
If there are no inserts being done on a table, then SELECT MAX(ID) should be fine. However, every database has a built-in function to return the most recently created primary key of a table after an insert has been performed. If that's what you're after, don't use MAX().
http://www.iknowkungfoo.com/blog/index.cfm/2008/6/1/Please-stop-using-SELECT-MAX-id
Also, for the id of the last record inserted, if you're using MySQLi, it would look like this:
$mysqli->insert_id
http://php.net/manual/en/mysqli.insert-id.php