Re: Getting last record ID created from DB
| From: | Colin Guthrie | Date: | Sat, 17 Mar 2007 14:12:20 +0000 |
| Subject: | Re: Getting last record ID created from DB | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-250752@lists.php.net to get a copy of this message | ||
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.