Re: Getting last record ID created from DB

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Mark wrote:
>> select user_id from t_users where user_id = (select max(user_id) from
>> t_users)
>>
> 
> It should be noted that that is the absolute 100% WRONG way to do it. If
> your site has any concurrency, you will almost certainly get the wrong
> user.

I agree 100% that it is 100% wrong, but just to point out something
blindingly obvious: it is also incredibly inefficient at being wrong!!

The sub-select selects the maximum user_id in the table. Fair enough.
This user_id is then fed into a statement that just returns that value
via a pointless lookup.

SELECT user_id from t_users WHERE user_id = 42;

Hmmmmm I wonder what user_id I'll get from that select....

Col.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux