Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3490026
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:27:58+00:00 2026-05-18T11:27:58+00:00

For few days now I’m trying to solve this problem. I have table group_user,

  • 0

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 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T11:27:59+00:00Added an answer on May 18, 2026 at 11:27 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying to solve this problem for a few days now without much
This problem has been kicking my butt for a few days now. I have
I have been having some problems with this for a few days now... I
been having this problem for a few days now and can't seem to find
I've been trying to figure this for a few days now, with no luck.
I have been trying to setup nginx with passenger for a few days now
I have a problem with NSNotificationCenter. Now it crashes, but a few days ago,
I've been working on this for a few days now, and I've found several
I have been using Subversion for a few days now and have a question...
This one has been driving me nuts for a few days now and I've

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.