0

我只想将 COUNT(page_ID) 值作为@current_rank,但我不得不更多地使用 MAX(rating_time),因为我可以有几条关于 WHERE 子句的“page_ID”记录,但在不同的日期(“rating_time”)。

在这些潜在的多个 page_ID 记录中,我想要最新的。

SET @current_rank = (SELECT COUNT(page_ID), MAX(rating_time)
    FROM ranks_update
    WHERE ranking_ID = NEW.ranking_ID
        AND page_ID <> NEW.page_ID 
        AND current_sum_vote >= @current_sum_vote
        AND rating_time >= ( *** a subquery *** ) 
    GROUP BY page_ID);

PS:我在 MySQL 中使用触发器。

4

3 回答 3

1

尝试

SELECT @current_rank = COUNT(page_ID), @max_rating = MAX(rating_time)
    FROM ranks_update
    WHERE ranking_ID = NEW.ranking_ID
        AND page_ID <> NEW.page_ID 
        AND current_sum_vote >= @current_sum_vote
    GROUP BY page_ID;
于 2012-08-03T03:42:11.137 回答
0

如果使用 MAX 函数的目的是过滤记录,您宁愿在子查询中放入 where 子句。如下所示:

SET @current_rank = (SELECT COUNT(page_ID)
FROM ranks_update
WHERE ranking_ID = NEW.ranking_ID
    AND page_ID <> NEW.page_ID 
    AND current_sum_vote >= @current_sum_vote
    AND rating_time in 
    (select MAX(rating_time) from ranks_update as ru where ru.page_ID = NEW.page_ID)
GROUP BY page_ID);
于 2012-08-03T04:12:08.363 回答
0

我认为解决方案是(如果我错了,请纠正我):

SELECT COUNT(page_ID)
    FROM (
    SELECT page_ID, MAX(rating_time)
    FROM ranks_update
    WHERE ranking_ID = NEW.ranking_ID
        AND page_ID <> NEW.page_ID 
        AND current_sum_vote >= @current_sum_vote
        AND rating_time >= (*** a subquery ***) 
    GROUP BY page_ID
    ) ru
于 2012-08-03T15:00:40.837 回答