[英]PHP mysql_insert_id() for MySQL UUID() primary keys?
Is there some equivalent to PHP mysql_insert_id
to fetch the last inserted UUID() primary key? 是否有与PHP
mysql_insert_id
等效的功能,以获取最后插入的UUID()主键? (I always get 0. It works for auto_inc integers though) (我总是得到0。尽管它适用于auto_inc整数)
No, last_insert_id() only retrieves that last generated auto_increment fields. 不,last_insert_id()仅检索最后生成的auto_increment字段。 You'll have to do a
select uuid()
first, then do an insert using that uuid. 您必须先执行
select uuid()
,然后使用该uuid进行插入。
However, note that uuids can't be guaranteed to be unique - they're simply very unlikely to collide. 但是,请注意,不能保证uuid是唯一的-它们几乎不可能碰撞。 If you do require uniqueness, then go with an auto_increment - they'll never be re-used within any single table.
如果您确实需要唯一性,则可以使用auto_increment-它们将永远不会在任何单个表中重复使用。
I found this quite short and simple solution: 我发现这个相当简短的解决方案:
set @id=UUID();
insert into <table>(<col1>,<col2>) values (@id,'another value');
select @id;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.