具有连接或子查询限制的 Select 语句

发布于 2024-10-06 10:13:39 字数 1132 浏览 1 评论 0原文

几天来我一直在努力解决这个问题。 我有表 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别再吹冷风 2024-10-13 10:13:39

创建示例表并添加数据:

CREATE TABLE `group_user` (
  `u_id` int(11) DEFAULT NULL,
  `g_id` int(11) DEFAULT NULL,
  `apply_date` date DEFAULT NULL
);

CREATE TABLE `group_name` (
  `g_id` int(11) DEFAULT NULL,
  `g_name` varchar(255) DEFAULT NULL
);

INSERT INTO `group_name` VALUES 
(1, 'Group 1'), (2, 'Group 2'), (3, 'Group 3'), (4, 'Group 4'), (5, 'Group 5');

INSERT INTO `group_user` VALUES
(1, 1, '2010-12-01'), (1, 2, '2010-12-01'), (1, 3, '2010-12-01'), (1, 4, '2010-12-01'), (1, 5, '2010-12-01'),
(2, 1, '2010-12-02'), (2, 2, '2010-12-02'),
(3, 1, '2010-12-03'), (3, 2, '2010-12-03'), (3, 3, '2010-12-03'), (3, 4, '2010-12-03'),
(4, 1, '2010-12-04'), (4, 2, '2010-12-04'),
(5, 1, '2010-12-05'), (5, 2, '2010-12-05'),
(6, 1, '2010-12-06'), (6, 2, '2010-12-06'),
(7, 1, '2010-12-07'), (7, 2, '2010-12-07'), (7, 3, '2010-12-07'), (7, 4, '2010-12-07'), (7, 5, '2010-12-07'),
(8, 1, '2010-12-08'), (8, 2, '2010-12-08'),
(9, 1, '2010-12-09'), (9, 2, '2010-12-09'), (9, 3, '2010-12-09'), (9, 4, '2010-12-09'), (9, 5, '2010-12-09');

选择用户 u_id == 1 所属的组。然后,为每个组选择最多 4 名成员(不包括用户 u_id == 1),按 apply_date 降序排列:

SELECT u3.g_id, g.g_name, u3.u_id, u3.apply_date
FROM (
  SELECT
    u1.g_id,
    u1.u_id,
    u1.apply_date,
    IF( @prev_gid <> u1.g_id, @user_index := 1, @user_index := @user_index + 1 ) AS user_index,
    @prev_gid := u1.g_id AS prev_gid
  FROM group_user AS u1
  JOIN (SELECT @prev_gid := 0, @user_index := NULL) AS vars
  JOIN group_user AS u2
  ON u2.g_id = u1.g_id
  AND u2.u_id = 1
  AND u1.u_id <> 1
  ORDER BY u1.g_id, u1.apply_date DESC, u1.u_id
) AS u3
JOIN group_name AS g ON g.g_id = u3.g_id
WHERE u3.user_index <= 4
ORDER BY u3.g_id, u3.apply_date DESC, u3.u_id;

+------+---------+------+------------+
| g_id | g_name  | u_id | apply_date |
+------+---------+------+------------+
|    1 | Group 1 |    5 | 2010-12-05 |
|    1 | Group 1 |    4 | 2010-12-04 |
|    1 | Group 1 |    3 | 2010-12-03 |
|    1 | Group 1 |    2 | 2010-12-02 |
|    2 | Group 2 |    5 | 2010-12-05 |
|    2 | Group 2 |    4 | 2010-12-04 |
|    2 | Group 2 |    3 | 2010-12-03 |
|    2 | Group 2 |    2 | 2010-12-02 |
|    3 | Group 3 |    9 | 2010-12-09 |
|    3 | Group 3 |    7 | 2010-12-07 |
|    3 | Group 3 |    3 | 2010-12-03 |
|    4 | Group 4 |    9 | 2010-12-09 |
|    4 | Group 4 |    7 | 2010-12-07 |
|    4 | Group 4 |    3 | 2010-12-03 |
|    5 | Group 5 |    9 | 2010-12-09 |
|    5 | Group 5 |    7 | 2010-12-07 |
+------+---------+------+------------+

Create sample tables and add data:

CREATE TABLE `group_user` (
  `u_id` int(11) DEFAULT NULL,
  `g_id` int(11) DEFAULT NULL,
  `apply_date` date DEFAULT NULL
);

CREATE TABLE `group_name` (
  `g_id` int(11) DEFAULT NULL,
  `g_name` varchar(255) DEFAULT NULL
);

INSERT INTO `group_name` VALUES 
(1, 'Group 1'), (2, 'Group 2'), (3, 'Group 3'), (4, 'Group 4'), (5, 'Group 5');

INSERT INTO `group_user` VALUES
(1, 1, '2010-12-01'), (1, 2, '2010-12-01'), (1, 3, '2010-12-01'), (1, 4, '2010-12-01'), (1, 5, '2010-12-01'),
(2, 1, '2010-12-02'), (2, 2, '2010-12-02'),
(3, 1, '2010-12-03'), (3, 2, '2010-12-03'), (3, 3, '2010-12-03'), (3, 4, '2010-12-03'),
(4, 1, '2010-12-04'), (4, 2, '2010-12-04'),
(5, 1, '2010-12-05'), (5, 2, '2010-12-05'),
(6, 1, '2010-12-06'), (6, 2, '2010-12-06'),
(7, 1, '2010-12-07'), (7, 2, '2010-12-07'), (7, 3, '2010-12-07'), (7, 4, '2010-12-07'), (7, 5, '2010-12-07'),
(8, 1, '2010-12-08'), (8, 2, '2010-12-08'),
(9, 1, '2010-12-09'), (9, 2, '2010-12-09'), (9, 3, '2010-12-09'), (9, 4, '2010-12-09'), (9, 5, '2010-12-09');

Select the groups of which user u_id == 1 is a member. Then for each group select a maximum of 4 members (excluding user u_id == 1), ordered by descending apply_date:

SELECT u3.g_id, g.g_name, u3.u_id, u3.apply_date
FROM (
  SELECT
    u1.g_id,
    u1.u_id,
    u1.apply_date,
    IF( @prev_gid <> u1.g_id, @user_index := 1, @user_index := @user_index + 1 ) AS user_index,
    @prev_gid := u1.g_id AS prev_gid
  FROM group_user AS u1
  JOIN (SELECT @prev_gid := 0, @user_index := NULL) AS vars
  JOIN group_user AS u2
  ON u2.g_id = u1.g_id
  AND u2.u_id = 1
  AND u1.u_id <> 1
  ORDER BY u1.g_id, u1.apply_date DESC, u1.u_id
) AS u3
JOIN group_name AS g ON g.g_id = u3.g_id
WHERE u3.user_index <= 4
ORDER BY u3.g_id, u3.apply_date DESC, u3.u_id;

+------+---------+------+------------+
| g_id | g_name  | u_id | apply_date |
+------+---------+------+------------+
|    1 | Group 1 |    5 | 2010-12-05 |
|    1 | Group 1 |    4 | 2010-12-04 |
|    1 | Group 1 |    3 | 2010-12-03 |
|    1 | Group 1 |    2 | 2010-12-02 |
|    2 | Group 2 |    5 | 2010-12-05 |
|    2 | Group 2 |    4 | 2010-12-04 |
|    2 | Group 2 |    3 | 2010-12-03 |
|    2 | Group 2 |    2 | 2010-12-02 |
|    3 | Group 3 |    9 | 2010-12-09 |
|    3 | Group 3 |    7 | 2010-12-07 |
|    3 | Group 3 |    3 | 2010-12-03 |
|    4 | Group 4 |    9 | 2010-12-09 |
|    4 | Group 4 |    7 | 2010-12-07 |
|    4 | Group 4 |    3 | 2010-12-03 |
|    5 | Group 5 |    9 | 2010-12-09 |
|    5 | Group 5 |    7 | 2010-12-07 |
+------+---------+------+------------+
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文