0

我有一张如下表:

content_id | contact_count
         1              23
         2               4
         3              89

我想从表的最后 25 行中选择 contact_count 最高的 content_id。

我尝试了许多不同的方法,例如:

select content_id from research_products_content
   where contact_count=(select max(contact_count)
        from research_products_content order by rating_total_id desc limit 25)
   order by rating_total_id desc limit 1
4

2 回答 2

0

由于列号将相同,因此使用 UNION

select content_id from research_products_content
   where contact_count=(select max(contact_count)
        from research_products_content order by rating_total_id desc limit 25)
UNION
select content_id from research_products_content
   where contact_count=(select max(contact_count)
        from research_products_content order by rating_total_id desc limit 1

您可能希望在此过程中实现缓存

于 2012-04-28T23:28:40.493 回答
0

在您的示例中,在选择结果(最大值,即单行)limit 25应用。试试这个:

SELECT tmp.content_id FROM (
  SELECT *
  FROM research_products_content
  ORDER BY rating_total_id DESC
  LIMIT 25
  ) AS tmp
WHERE tmp.contact_count = (
  SELECT max(tmp2.contact_count)
  FROM (
    SELECT *
    FROM research_products_content
    ORDER BY rating_total_id DESC
    LIMIT 25
  ) AS tmp2
)
LIMIT 1
于 2012-04-28T23:30:48.377 回答