简体   繁体   English

如何获取上次mysql取数据的值? PHP-MySQL

[英]How to get the value of last mysql fetch data? PHP-MySQL

I want to do this: 我想做这个:

Check the top 10 values of points. 检查点的前10个值。

Here is my condition: 这是我的情况:

  • If there are less than 10 records (rows) found, eg give bonus 如果发现少于10条记录(行),例如给予奖励

  • If the points is in top ten (among hundreds records/rows), give bonus. 如果得分排在前十位(在数百条记录/行中),则给予奖励。

So, what i do is (wrong method): 所以,我要做的是(错误的方法):

SELECT points FROM `scores` WHERE id = '1' ORDER BY score DESC LIMIT 9 , 1

That will only work if i have more then 9 (at least 10) data/records. 只有当我有9(至少10)个数据/记录时,这才起作用。

Is there any other way? 还有其他办法吗?

I am thinking of using this(not very good though): 我正在考虑使用此(虽然不是很好):

 SELECT points FROM `scores` WHERE id = '1' ORDER BY score DESC LIMIT 0 , 10

Then get the last value of mysql_fetch_assoc data. 然后获取mysql_fetch_assoc数据的最后一个值。 Thus, how do I get the last value of mysql_fetch_assoc data? 因此,如何获取mysql_fetch_assoc数据的最后一个值?

The previous query is close to what you want: 上一个查询接近您想要的:

SELECT points FROM (
  SELECT points, score 
  FROM scores 
  WHERE id = '1' 
  ORDER BY score DESC 
  LIMIT 10
) AS top_ten 
ORDER BY score ASC 
LIMIT 1

If you want to stick with mysql_fetch_assoc getting the last value you can utilize mysql_data_seek. 如果要坚持使用mysql_fetch_assoc获取最后一个值,可以使用mysql_data_seek。

Something like this: 像这样:

<?php
// ... $result is the result set from your query

$result_count = mysql_num_rows($result);
if ($result_count > 0)
{
    mysql_data_seek($result, $result_count - 1);
}

$row = mysql_fetch_assoc($result);
?>

try to use this: 尝试使用此:

SELECT points FROM (SELECT points FROM scores WHERE id = '1' ORDER BY score DESC LIMIT 10) ORDER BY score LIMIT 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM