我有一个列表user_id
的整数,例如N
。MySQL:以任何方式将这些N个查询变成更少的查询?
[1001, 1023, 13452, 1679834, ...]
和表:
CREATE TABLE content (
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
user_id INT,
content VARCHAR(100),
score INT
);
我需要那些N
整数从user_id
和每个user_id
获得前3 content
具有最高score
。所以基本上我需要运行此查询N
时间:
SELECT *
FROM content
WHERE user_id=1001
ORDER BY score DESC
LIMIT 3;
N
可能是一个非常大的数字。所以我非常想避免一个一个地运行这些查询。
有什么办法可以减少我需要运行的查询次数吗?某种批量选择也许?
通过“每个组最大的标签”标签 – newtover 2012-03-21 21:52:21
是的,例如http://stackoverflow.com/questions/5319643/top-n-per-group-with-multiple-table-joins – Daan 2012-03-21 21:56:08
另一个:http://stackoverflow.com/questions/1442527/how-to-select-the-newest-four-items-per-category/1442867#1442867 – newtover 2012-03-21 22:25:28