[英]Select previous row mysql?
If I have a mysql table which had primary ids and another field called gameScore can I do something along the lines of... 如果我有一个具有主要ID的mysql表和另一个名为gameScore的字段,我可以按照以下方式进行操作...
SELECT gameScore FROM table1 WHERE id = 100 ORDER BY gameScore ASC
This will get the gameScore for the id 100, but what I would like to do is have mysql order the table by gameScore and then select the previous row to where the id = 100, if that makes sense thanks... 这将获得id为100的gameScore,但是我想让mysql按gameScore排序表,然后选择id = 100的上一行,如果可以的话,谢谢...
looks funny - but maybe this is what you're after. 看起来很有趣-但这也许就是您所追求的。
select max( gameScore )
from table1
where id = 100
and gameScore < ( select max( gameScore ) from table1 where id = 100 )
我建议做一个联合而不是AND(此外,您需要添加LIMIT 1,这样您就不会得到所有小于100的行)
You're looking for the second lowest score, right? 您正在寻找第二低的分数,对吧? Like Craig mentioned, limit sounds like the right way to go:
就像克雷格(Craig)提到的那样,限制听起来像是正确的做法:
SELECT
gameScore
FROM
table1
WHERE
id = 100
ORDER BY
gameScore ASC
limit 1,1
Something like this - 像这样-
SELECT t1.* FROM table1 t1
JOIN (
SELECT id, MAX(gameScore) gameScore FROM table1
WHERE id = 100 AND gameScore < 50 ORDER BY gameScore
) t2
On t1.id = t2.id AND t1.gameScore = t2.gameScore
To find previous record we need to select exact current record, in example it is a record with id
= 100 and gameScore
= 50. 要查找以前的记录,我们需要选择确切的当前记录,例如,它是
id
= 100且gameScore
= 50的记录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.