开发者

Select statement with join, or subquery limit

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 u开发者_开发问答serid 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 


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 |
+------+---------+------+------------+
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜