如何在插入时获取插入行的ID? [英] How to get the id of an inserted row ON insert?

查看:97
本文介绍了如何在插入时获取插入行的ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在插入该行时为AUTO-INCREMENT字段创建唯一的哈希.像这样:

I would like to create a unique hash of an AUTO-INCREMENT field while inserting that row. Something like:

INSERT INTO `table` (`hash`,`salt`) VALUES (MD5(CONCAT(`id`,`salt`)),'1234789687');

是否可以使用我要插入的行的AUTO-INCREMENTED ID?还是我必须等到它插入后才能通过PHP或其他方式来获取它?

Is it possible to use the AUTO-INCREMENTED id of the row WHILE I'm inserting? Or do I have to wait until it's inserted to grab it via PHP or something?

推荐答案

简短的回答是否"-这是不可能的.如果您的数据库交易量低,那么使用类似于select max()的东西(易受干扰)并发可能不是问题:

The short answer is no- it is not possible. If your db has low transaction volume, then concurrency may not be an issue to use something similar to select max() (susceptible to interference) : Can you access the auto increment value in MySQL within one statement?

我将在php中的插入之后进行更新

I would follow the insert in php with an update

update `table`
set `hash` = MD5(CONCAT(`id`,`salt`))
where `id` = LAST_INSERT_ID();

这篇关于如何在插入时获取插入行的ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆