检索 mysql 中最后更新的列

发布于 2024-07-26 17:12:56 字数 422 浏览 5 评论 0原文

我有一个 MySQL 查询,如下所示(使用 Zend_Db):(

 $sql = $handle->quoteInto("UPDATE board SET rank=rank+1 WHERE post_id=?", $postid);
 $handle->query($sql);

排名不是自动递增的 PK)。 我现在想检索 rank 的值,而不执行另一个查询。 我已经尝试过 $handle->lastInsertId(); 但它似乎不起作用,因为我没有使用 MySQL 的自然自动递增方法(我不能 - 排名 是帖子的排名,我要么 ++ 要么 -- 它。)

有什么方法可以通过执行另一个查询来做到这一点? 将返回最后更改的值的函数?

I have a MySQL query that goes as follows (using Zend_Db):

 $sql = $handle->quoteInto("UPDATE board SET rank=rank+1 WHERE post_id=?", $postid);
 $handle->query($sql);

(Rank isn't an auto-incrementing PK).
I would like to now retrieve the value of rank without preforming another query.
I've tried $handle->lastInsertId(); but it doesn't seem to work , since I didn't use MySQL's natural auto-incrementing method (I can't - rank is the rank of a post. I either ++ or -- it.)

Any way to do this with preforming another query? A function that will return the last changed value?

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

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

发布评论

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

评论(2

离线来电— 2024-08-02 17:12:56

我不相信这是可能的 - 你只需要执行SELECT

I don't believe this is possible - you'll just have to do a SELECT.

纵山崖 2024-08-02 17:12:56

您可以使用LAST_INSERT_ID MySQL 提供的用于设置值的函数,然后通过 mysql_insert_id() $handle->lastInsertId(); 依赖的 C 函数。

以下是代码片段的更新版本,其中包含对其进行的 LAST_INSERT_ID 更改:

 $sql = $handle->quoteInto("UPDATE board SET rank=LAST_INSERT_ID(rank+1) WHERE post_id=?", $postid);
 $handle->query($sql);

如果您有任何疑问,请告诉我。

HTH,

-迪平

You can use the LAST_INSERT_ID function that MySQL provides to set a value and then make it available via the mysql_insert_id() C function that the $handle->lastInsertId(); relies on.

The following is an updated version of your code snippet with the LAST_INSERT_ID changes made to it:

 $sql = $handle->quoteInto("UPDATE board SET rank=LAST_INSERT_ID(rank+1) WHERE post_id=?", $postid);
 $handle->query($sql);

Let me know if you have any questions.

HTH,

-Dipin

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