Mysql 应用多重限制
我有一张表,如下所示:Mysql 应用多重限制,mysql,Mysql,我有一张表,如下所示: content_id | contact_count 1 23 2 4 3 89 我想从表的最后25行中选择联系人计数最高的内容id 我尝试过许多不同的方法,例如: select content_id from research_products_content where contact_count=(select max(con
content_id | contact_count
1 23
2 4
3 89
我想从表的最后25行中选择联系人计数最高的内容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
由于列号相同,请使用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
您可能希望在此过程中实现缓存在您的示例中,在选择结果(最大值,是一行)后应用
限制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
如何定义最后25行?最高的
联系人id
?哇,我太感谢你了!我已经为这个问题挣扎了一段时间了!出于好奇,由于我对Mysql不太熟悉(但我开始做大量php开发),您能推荐一些好的SQL资源吗?我很高兴能提供帮助。考虑将答案标记为“接受”:关于SQL资源,我从来没有读过任何书籍,也没有使用任何特定的网站(当然,当然除外)。只是在谷歌上四处搜索,阅读不同的文章。有时甚至维基百科也是一个很好的起点:)谢谢Starx!我计划在未来几天内在我的PHP服务器上实现memcached!