当前位置: 动力学知识库 > 问答 > 编程问答 >

php - Update field if the ORDER BY is 1 descending?

问题描述:

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?

网友答案:

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
网友答案:

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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: