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 8056255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:39:08+00:00 2026-06-05T08:39:08+00:00

I have a table, two columns, user and group . It’s a many-to-many relationship,

  • 0

I have a table, two columns, user and group. It’s a many-to-many relationship, so we might have…

group |  user
1     |  1
1     |  2
1     |  3
2     |  1
2     |  3
3     |  1
3     |  4
4     |  2
4     |  3

… and so on.

I need to get a list of users in all the groups which contain a particular a user ID. So for the above, if I gave it user ID 3, it would first get all the groups for that (in this case, 1, 2, and 4), and then return all the users for those groups (in this case, 1, 2, and 3). Right now, this is accomplished through two nested selects:

SELECT * FROM other_table
WHERE user_id IN (
    SELECT user_id FROM table WHERE group_id IN (
        SELECT group_id FROM table WHERE user_id = 3
    )
)

This is proving horribly inefficient. Is there something obvious I’m missing here?

Even if I run the query on its own, it runs very inefficiently:

SELECT user_id FROM table WHERE group_id IN (
    SELECT group_id FROM table WHERE user_id = 3
)

Edit: Here’s the CREATE TABLE for the table.

CREATE TABLE `user_groups` (
  `user_id` bigint(12) unsigned NOT NULL,
  `group_id` bigint(12) unsigned NOT NULL,
  PRIMARY KEY  (`user_id`,`group_id`),
  KEY `user_id` (`user_id`),
  KEY `group_id` (`group_id`),
) ENGINE=MyISAM DEFAULT CHARSET=latin1

Edit: And the explain of what happens with the query in the answer below:

id | select_type | table | type   | possible_keys            | key      | key_len | ref         | rows | Extra
1  | SIMPLE      | t1    | ref    | PRIMARY,user_id,group_id | PRIMARY  | 8       | const       | 839  | Using index
1  | SIMPLE      | t2    | ref    | PRIMARY,user_id,group_id | group_id | 8       | t1.group_id | 2331 | 
1  | SIMPLE      | users | eq_ref | PRIMARY                  | PRIMARY  | 8       | t2.user_id  | 1    |
  • 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-06-05T08:39:09+00:00Added an answer on June 5, 2026 at 8:39 am

    Just use a self-join:

    SELECT other_table.*
    FROM   `table` t1
      JOIN `table` t2 USING (group_id)
      JOIN other_table ON other_table.user_id = t2.user_id
    WHERE  t1.user_id = 3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two models, Users and Groups. Each group can have many users and
I have a SQL table with two columns Date and User and contains the
I have one table and I need to check if two users, for whom
I have two tables, users and groups . A user can belong to many
I have a database test with table name table with two columns user &
I have table with two columns (startdate & enddate) and I have two submission
I have a table with two columns. Items in the first column represent one
I have a table with two columns and one row, and 100% width across
I have a table with two columns: column A column B 1 2 1
I have a table with two columns, GroupId and ParentId (both are GUIDS). The

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.