我有一个User
表,其中包含以下行
id: 1, favorite_id: 1
id: 2, favorite_id: 2
id: 3, favorite_id: 2
id: 4, favorite_id: 3
id: 5, favorite_id: 3
id: 6, favorite_id: 3
id: 7, favorite_id: 4
id: 8, favorite_id: 4
id: 9, favorite_id: 4
并寻找一个查询,该查询返回前3个不同的favorite_id
的行=>
id: 1, favorite_id: 1
id: 2, favorite_id: 2
id: 3, favorite_id: 2
id: 4, favorite_id: 3
id: 5, favorite_id: 3
id: 6, favorite_id: 3
答案 0 :(得分:1)
尝试:
SELECT t.*
FROM `User` t
JOIN (SELECT DISTINCT favorite_id
FROM `User`
ORDER BY favorite_id LIMIT 3) AS dt
ON dt.favorite_id = t.favorite_id
答案 1 :(得分:1)
如果用“前三个”表示按顺序Deferred<T>
出现的前三个,则
id
如果仅表示select u.*
from (select u.favorite_id, min(id) as min_id
from users u
group by u.favorite_id
order by min_id asc
limit 3
) f3 join
users u
on f3.favorite_id = u.favorite_id;
的三个最低值,那么Madhur的解决方案就可以了。