如果 ORDER BY 为 1 降序,则更新字段?

发布于 2024-11-17 17:32:43 字数 244 浏览 4 评论 0原文

有没有可能的方法来做到这一点:

我想根据 ORDER BY 位置更新字段“排名”。

例如:(伪代码)

If id order by place = 1 then update rank field to place were id=get id


 rank place id

  1     1    5   PC
  2     2    8   MAC

这可能吗?

Is there a possible way to do this:

I want to update a field 'rank' according to the ORDER BY place.

For example: (pseudocode)

If id order by place = 1 then update rank field to place were id=get id


 rank place id

  1     1    5   PC
  2     2    8   MAC

is this possible?

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

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

发布评论

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

评论(2

满地尘埃落定 2024-11-24 17:32:43

像这样的东西吗?

UPDATE tbl_name
SET rank = 1
WHERE id = (
     SELECT id
     WHERE condition
     ORDER BY place DESC
     LIMIT 1
)

或者从你的评论中(我认为MySQL http://dev.mysql.com /doc/refman/5.0/en/update.html):

UPDATE tbl_name
SET rank = 10
WHERE id = 9
ORDER BY wins DESC
LIMIT 1

您始终可以执行 SELECT 来检查这些是否也是您希望更新的记录:

SELECT *
FROM tbl_name
WHERE id = (
     SELECT id
     WHERE condition
     ORDER BY place DESC
     LIMIT 1
)

或者

SELECT *
FROM tbl_name
WHERE id = 9
ORDER BY wins DESC
LIMIT 1

Something like this?

UPDATE tbl_name
SET rank = 1
WHERE id = (
     SELECT id
     WHERE condition
     ORDER BY place DESC
     LIMIT 1
)

Or from your comment (I think MySQL http://dev.mysql.com/doc/refman/5.0/en/update.html):

UPDATE tbl_name
SET rank = 10
WHERE id = 9
ORDER BY wins DESC
LIMIT 1

You can always do a SELECT to check if these are the records you wish to UPDATE as well:

SELECT *
FROM tbl_name
WHERE id = (
     SELECT id
     WHERE condition
     ORDER BY place DESC
     LIMIT 1
)

OR

SELECT *
FROM tbl_name
WHERE id = 9
ORDER BY wins DESC
LIMIT 1
眼眸里的快感 2024-11-24 17:32:43

某些 RDBMS 在查询中具有 ROWNUM 伪列,您可以为此使用。
您没有指定您使用的数据库,例如Oracle就有这个。

Some RDBMS have a ROWNUM pseudocolumn in queries you can use for this.
You did not specify what database you are using, for example Oracle has this.

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