具有连接或子查询限制的 Select 语句
几天来我一直在努力解决这个问题。 我有表 group_user,group_name。 我想要做的是选择用户组,而不是描述该组(来自 group_name),以及该组中的其他 10 个用户。
前两个都没有问题。问题是,我无处获得限制用户。
我可以选择 user_group 以及该组中的其他用户。我不知道如何限制它。 使用:
SELECT a.g_id,b.group,b.userid
FROM group_user AS a
RIGHT JOIN
(SELECT g_id as group, u_id as userid FROM group_user) AS b ON a.g_id=b.group
WHERE u_id=112
它显示了我、我的用户组以及该组中的用户。但当我试图限制 subwuery 时,它限制了所有人,而不是特定的群体。
我尝试过,选择用户,并使用 IN 其中我的用户组没有运气。
我想也许团体和拥有会有所帮助,但我不知道如何使用它。
所以我的问题是,如何限制 MySQL 中的子查询结果,其中子查询是基于查询结果构建的。
我想我超载了,也许我没有看到什么。
更新以显示我真正想要完成的事情是另一段代码。
SELECT g_id FROM group_user WHERE user_id = 112
因此,我获取用户所在的所有组,假设每个选择都是 var extra_group,因此第二个查询将是
SELECT u_id FROM group_user WHERE group_id = extra_group LIMIT 10
我需要在一个查询中执行与上面相同的操作。
迈克发布后的另一个更新。 我应该补充一点,用户可以属于多个组。所以我认为真正的问题是,我不知道如何选择这些组,并在同一查询中为所选组选择 10 个用户,因此结果可能是
g_id u_id
1 | 2
1 | 3
1 | 4
3 | 3
3 | 8
g_id 是该查询中的用户组
SELECT g_id FROM group_user WHERE user_id = 112
For few days now I'm trying to solve this problem.
I have table group_user, group_name.
What I wanna to do is select user groups, than description that group (from group_name), and 10 other users from the group.
It's not problem with first two. The problem is, that I'm nowhere to get limit users.
I can select user_group, and other users in that group. I don't know how to limit that.
Using:
SELECT a.g_id,b.group,b.userid
FROM group_user AS a
RIGHT JOIN
(SELECT g_id as group, u_id as userid FROM group_user) AS b ON a.g_id=b.group
WHERE u_id=112
It showing me, my user groups and users in that group. But when I'm trying to limit in subwuery, it limits all, not particular group.
I tried, Select users, with using IN where was goups of my user without luck.
I was thinking maybe group and having will help, but I can't see how I could use it.
So my question is, how can I limit subquery result in MySQL where the subquery is built on result of query.
I think im overload and maybe I don't see something.
UPDATE to show what I really wanna accomplish here's another piece of code.
SELECT g_id FROM group_user WHERE user_id = 112
So I get all groups that user is in let, saye each of that select is var extra_group, so second query will be
SELECT u_id FROM group_user WHERE group_id = extra_group LIMIT 10
I need to do same as above, in one query.
another UPDATE after MIKE post.
I should ADD that, user can be in more than 1 group. So I think the real problem is, that I don't have any clue how to select those groups and in same query select 10 users for selected groups, so in result could be
g_id u_id
1 | 2
1 | 3
1 | 4
3 | 3
3 | 8
where g_id is user groups from that query
SELECT g_id FROM group_user WHERE user_id = 112
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
创建示例表并添加数据:
选择用户
u_id == 1
所属的组。然后,为每个组选择最多 4 名成员(不包括用户u_id == 1
),按apply_date
降序排列:Create sample tables and add data:
Select the groups of which user
u_id == 1
is a member. Then for each group select a maximum of 4 members (excluding useru_id == 1
), ordered by descendingapply_date
: