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

  • SEARCH
  • Home
  • 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 7189019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:12:43+00:00 2026-05-28T19:12:43+00:00

The table: id | category (there is a index on id & category) ———–

  • 0

The table:

id | category   (there is a index on id & category)
-----------
1  |  1
1  |  7
1  |  3

2  |  1
2  |  2
2  |  4

3  |  1
3  |  6
3  |  3

SELECT DISTINCT
    category
FROM 
    many_to_many e1     
WHERE id IN 
    (
    SELECT DISTINCT
        e1.id
    FROM 
        many_to_many e1           
    INNER JOIN
        many_to_many x1 
      ON e1.id=x1.id
    WHERE
        e1.category IN (3)
    )

I like to get retured: ** 6, 1, 7** (what I do get with the query above)
It seems to me this query is not gone preform well, because the sub query searches all the ID’s and this list can be huge.

Also it doesn’t matter how many but if the ID is related.
So for performance if there would be 100 id’s checking once for each unique category to be populated would be enough.

Secondly I use an other query (the sub query )the get all id’s who contain the category:

SELECT DISTINCT
        e1.id
    FROM 
        many_to_many e1           
    INNER JOIN
        many_to_many x1 
      ON e1.id=x1.id
    WHERE
        e1.category IN (3)

 returns: 3 & 1

What would be the most efficient way to query the result I like. Is there an more efficient (better preforming) solution?
Should I use one instead of two query’s?

  • 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-28T19:12:44+00:00Added an answer on May 28, 2026 at 7:12 pm
    SELECT DISTINCT
            x1.category
        FROM 
            many_to_many e1           
        INNER JOIN
            many_to_many x1 
          ON e1.id = x1.id
        WHERE
            e1.category = 3          --- IN (3) 
          AND
            x1.category <> 3         --- NOT IN (3)
    

    You shouldl check the plan of the query with EXPLAIN. A compound index on (category, id) might be good for this query.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have follow query: SELECT tbl_yaca_sites.`id_site` FROM `tbl_yaca_sites` INNER JOIN `tbl_relation` ON tbl_relation.id_website = tbl_yaca_sites.id_site
table data of 2 columns category and subcategory i want to get a collection
I need to select a top row for each category from a known set
So I have a category table set up. Within this table there is a
The category table looks like somewhat as below: id -- name -- parent_id 1
Consider a table with 3 columns: ID (unique, taken from an Oracle sequence), CATEGORY
Is there a simple way to get the user's selection (the string value) from
I have three tables Category, Movies and RelCatMov Category-table categoryid, categoryName 1 thriller 2
If I have two tables... Category and Pet. Is there a way in LINQ
I have the following hierarchical table: Table Category: CategoryId, ParentCategoryId, CategoryName 1, null, SomeRoot

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.