我搜索了很多,但我找不到答案或附近的东西。
我有一张这样的表:
id int(11) NO PRI auto_increment
attribute_id int(11) YES
user_id int(11) YES
result int(11) YES
x10 int(11) YES
每个attribute_id
我需要大约5个结果。
attribute_id
可以是任意数字,因此首先,我需要检查attribute_id
,然后我必须检查每个attribute_id的结果。
我不知道如何在没有MySQL之外的任何编程语言的情况下这样做,但我知道你可以。
示例:
attribute_id result
1 200
1 149
1 123
2 322
2 321
2 300
3 ...
3 ...
等等。
答案 0 :(得分:1)
SELECT attribute_id, result
FROM TableName a
WHERE
(
SELECT COUNT(*)
FROM TableName b
WHERE a.attribute_id = b.attribute_id
AND a.result <= b.result
) <= 5
答案 1 :(得分:0)
SELECT attribute_id, result
FROM TableName a
WHERE
(
SELECT COUNT(*)
FROM TableName b
WHERE a.attribute_id = b.attribute_id
AND a.result <= b.result
) <= 5 order by result DESC;