我需要知道表中的最后5个条目是否由相同的用户ID放置(在用户ID下面的代码中是frozen_by
。
这就是我所拥有的
//count if the last 5 freezes was placed by the same user
$strFind="SELECT COUNT(*) AS `total` FROM `contest_frozen` WHERE `contest_id`=\"$pid\" AND `frozen_by`=\"$curmemid\" ORDER BY `id` DESC LIMIT 5";
$result=mysql_query($strFind) or die(mysql_error());
$row=mysql_fetch_array($result);
$freeze_total=$row['total'];
if($freeze_total>=5){
//do this
}
if($freeze_total<5){
//do this
}
答案 0 :(得分:1)
您可以使用如下查询:它返回最后5行中不同用户的数量。它还返回最高的用户ID,因此如果UserCount == 1
和MaxUser == $curmemid
,则您知道该用户放置了最后五个记录。
SELECT
count(distinct frozen_by) as UserCount,
max(frozen_by) as MaxUser
FROM
contest_frozen
WHERE
contest_id = $pid
ORDER BY
`id` DESC
LIMIT 5
如果您需要明确的计数,您也可以这样做。下面的查询返回给定用户的记录数和其他用户的记录数。如果ThisUserCount == 5则所有5条记录都来自该用户。
SELECT
sum(case when frozen_by = $curmemid then 1 else 0 end) as ThisUserCount,
sum(case when frozen_by = $curmemid then 0 else 1 end) as OtherUserCount
FROM
contest_frozen
WHERE
contest_id = $pid
ORDER BY
`id` DESC
LIMIT 5