简体   繁体   English

在MySQL中限制分组依据

[英]Limit on Group by in Mysql

In my web application, I would like to show a list of my users. 在我的Web应用程序中,我想显示我的用户列表。 Users can have different statuses (FB-like). 用户可以具有不同的状态(类似于FB)。 I want to display the last 3 statuses below each user's name. 我想在每个用户名下方显示最后3个状态。

Dinosaur
   I'm a dino!
   I eat meat!
   I like cows!
Fish
   Blub!
   I don't like dinosaurs!
   Going for a swim!

I have the following SQL query: 我有以下SQL查询:

SELECT s.status, u.voornaam, u.achternaam FROM status AS s INNER JOIN sportjefit_user AS u ON s.user = u.id where u.begeleider='53' group by id desc limit 3

However, this returns only the top 3 of the results, in this case it would only show the Dinosaur's statuses. 但是,这仅返回结果的前3位,在这种情况下,它将仅显示恐龙的状态。 I want to show the top 3 statuses for every users though. 我想显示每个用户的前3个状态。 Can I do this with a group by and put a limit on this or do I need multiple queries here? 我可以通过group by并对此加以limit吗?还是在这里需要多个查询? And, if I do need multiple queries, how would I go about implementing this? 而且,如果我确实需要多个查询,我将如何实现它? The number of users will keep increasing as my application grows. 用户数量将随着我的应用程序的增长而不断增加。

Can someone help me out? 有人可以帮我吗?

Thanks. 谢谢。

I'd use multiple queries like this: 我会使用多个这样的查询:

select all users
for each user
    select last 3 statuses

using 运用

SELECT * FROM sportjefit_user

and

SELECT status FROM status WHERE userid = ? ORDER BY id DESC LIMIT 3

How many users do you anticipate? 您预计有多少用户? If you're trying to show all users on a page, I'd paginate them. 如果您要在页面上显示所有用户,请对它们进行分页。 Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM