PHP mysql_insert_id() for MySQL UUID() primary key

2019-06-10 00:16发布

Is there some equivalent to PHP mysql_insert_id to fetch the last inserted UUID() primary key? (I always get 0. It works for auto_inc integers though)

2条回答
来,给爷笑一个
2楼-- · 2019-06-10 00:47

I found this quite short and simple solution:

set @id=UUID();
insert into <table>(<col1>,<col2>) values (@id,'another value');
select @id;
查看更多
女痞
3楼-- · 2019-06-10 00:51

No, last_insert_id() only retrieves that last generated auto_increment fields. You'll have to do a select uuid() first, then do an insert using that uuid.

However, note that uuids can't be guaranteed to be unique - they're simply very unlikely to collide. If you do require uniqueness, then go with an auto_increment - they'll never be re-used within any single table.

查看更多
登录 后发表回答