-1

我正在处理用户活动,所以在这种情况下,我需要检查最后 30 行中用户的最后一个活动。

我尝试使用它,但这将查看所有表格行,但由于我只需要检查最后 30 行,因此我需要帮助。

$CheckQuery = mysql_query("SELECT * FROM activities WHERE got_id='$user->id' ORDER BY `id` DESC LIMIT 0 , 1");

while($row = mysql_fetch_assoc($CheckQuery))
{
    // some function
}

这就是我想知道的:)

select *
from (select *
      from activities
      order by `id` desc
      limit 30) t
where got_id = '$user->id'
limit 1

正如我所说:我需要检查最后 30 行中用户的最后一个活动(不是活动)。

4

4 回答 4

4
SELECT * FROM activities WHERE got_id='$user->id' ORDER BY `id` DESC LIMIT  30"

请参阅此处以限制您的查询:http ://dev.mysql.com/doc/refman/5.6/en/limit-optimization.html

于 2013-03-18T10:22:51.223 回答
4

这是正确的方法,您只能limit 30使用

SELECT * FROM activities WHERE got_id='$user->id' ORDER BY `id` DESC LIMIT 30

更新

如果您希望用户的最后一个活动在最后 30 个活动中,则必须首先选择最后 30 个活动并从中选择用户的最后一个活动

select *
from (select *
      from activities
      order by `id` desc
      limit 30) t
where got_id = '$user->id'
limit 1

但如果用户最近不活跃,这可能会给出一个空的结果集。

于 2013-03-18T10:23:26.647 回答
4

只需按降序计数选择最后 30 行。查看此查询:-

"Select * from activities WHERE got_id='$user->id' ORDER BY `id` DESC limit  30"
于 2013-03-18T10:23:56.377 回答
3

试试这个,

$CheckQuery = mysql_query("SELECT * FROM activities WHERE got_id='$user->id' ORDER BY `id` DESC LIMIT 30");
于 2013-03-18T10:22:38.253 回答