直接在MySQL语句中生成MD5 idHash

发布于 2024-11-06 22:35:23 字数 238 浏览 0 评论 0原文

在我的表中,我有一个自动递增的用户ID。在同一行我有一个 idHash。是否可以使用相同的 INSERT 语句直接生成 idHash(简单的 MD5 和),这样我就不必选择 id,然后再次更新 idHash?

问题是:在 MySQL 生成(自动递增)之前我不知道 userID。

谢谢 弗兰克

PS:我正在使用 PHP。 PPS:这个问题都是关于单个插入的。我知道我可以使用PHP或其他语言手动选择数据然后更新它。

In my table I have an userID that is auto-incremented. In the same row I have an idHash. Is it possible to generate the idHash (simply an MD5 sum) from it directly with the same INSERT statement so that I don't have to SELECT the id, and then UPDATE the idHash again?

Problem is: I do not know the userID before it is being generated (auto-incremented) by MySQL.

Thanks
Frank

PS: I'm using PHP.
PPS: This question is all about a SINGLE INSERT. I know that I can use PHP or other languages to manually select the data and then update it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

甲如呢乙后呢 2024-11-13 22:35:23

我不相信您可以在单个 INSERT 语句中完成此操作。

您可能可以做的是使用INSERT触发器,它既确定新的ID,对其进行散列,又然后更新记录。

I don't believe you can do it within a single INSERT statement.

What you probably could do is use an INSERT trigger, that both determines the new ID, hashes it, and then updates the record.

时光磨忆 2024-11-13 22:35:23

我可以推荐的一种解决方案是使用最后一个插入 ID,而不是重新查询表。这是一个简化的示例:

<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$query = "INSERT INTO users VALUES (....)";
$mysqli->query($query);

$newUserID = $mysqli->insert_id;

$query = "UPDATE users SET idHash = MD5(userID) WHERE userID = $newUserID";
$mysqli->query($query);

/* close connection */
$mysqli->close();
?>

One solution I can recommend is using the last insert ID instead of re-querying the table. Here is a simplified example:

<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$query = "INSERT INTO users VALUES (....)";
$mysqli->query($query);

$newUserID = $mysqli->insert_id;

$query = "UPDATE users SET idHash = MD5(userID) WHERE userID = $newUserID";
$mysqli->query($query);

/* close connection */
$mysqli->close();
?>
镜花水月 2024-11-13 22:35:23

据我所知,如果您使用 auto_increment,则没有“安全”方法可以在同一查询中执行此操作。

但是,如果表中的行从未被删除,您可以使用这个小技巧:

insert into mytable (col1, col2, col3, idhash) 
values ('', '', '', md5(select max(id) from mytable))

我不明白为什么您需要对 id 进行哈希处理,为什么不使用 id > 直接?

AFAIK there's no "secure" way for doing this in the same query if you're using auto_increment.

However, if rows are never deleted in your table, you can use this little trick :

insert into mytable (col1, col2, col3, idhash) 
values ('', '', '', md5(select max(id) from mytable))

I don't understand why you need to hash the id though, why not use the id directly ?

不离久伴 2024-11-13 22:35:23

这似乎对我有用:

CREATE TABLE tbl (id INT PRIMARY KEY AUTO_INCREMENT, idHash TEXT);
INSERT INTO tbl (idHash) VALUES (MD5(LAST_INSERT_ID() + 1));
SELECT *, MD5(id) FROM tbl;

请注意,这仅适用于单行插入,因为 LAST_INSERT_ID 返回插入的第一行的插入 ID。

对 auto_increment 值执行 MD5(column_name) 不起作用,因为该值尚未生成,因此它本质上是调用 MD5(0)

This seems to work for me:

CREATE TABLE tbl (id INT PRIMARY KEY AUTO_INCREMENT, idHash TEXT);
INSERT INTO tbl (idHash) VALUES (MD5(LAST_INSERT_ID() + 1));
SELECT *, MD5(id) FROM tbl;

Note this will only work on single-row inserts as LAST_INSERT_ID returns the insert ID of the first row inserted.

Performing MD5(column_name) on an auto_increment value does not work as the value has not been generated yet, so it is essentially calling MD5(0).

最美不过初阳 2024-11-13 22:35:23

PHP 片段

<?
$tablename      = "tablename";
$next_increment     = 0;
$qShowStatus        = "SHOW TABLE STATUS LIKE '$tablename'";
$qShowStatusResult  = mysql_query($qShowStatus) or die ( "Query failed: " . mysql_error() . "<br/>" . $qShowStatus );

$row = mysql_fetch_assoc($qShowStatusResult);
$next_increment = $row['Auto_increment'];

echo "next increment number: [$next_increment]";
?>

这将为您提供下一个自动增量,然后您可以在插入中使用它。

注意:这并不完美(您的方法并不完美,因为您实际上有 2 个主键)

来自:http: //blog.jamiedoris.com/geek/560/

PHP snippet

<?
$tablename      = "tablename";
$next_increment     = 0;
$qShowStatus        = "SHOW TABLE STATUS LIKE '$tablename'";
$qShowStatusResult  = mysql_query($qShowStatus) or die ( "Query failed: " . mysql_error() . "<br/>" . $qShowStatus );

$row = mysql_fetch_assoc($qShowStatusResult);
$next_increment = $row['Auto_increment'];

echo "next increment number: [$next_increment]";
?>

This will get you the next auto-increment and then you can use this in your insert.

Note: This is not perfect (Your method is imperfect as you will effectively have 2 primary keys)

From: http://blog.jamiedoris.com/geek/560/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文