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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:35:54+00:00 2026-06-16T18:35:54+00:00

I have seen many posts but didn’t get my answer i have a table

  • 0

I have seen many posts but didn’t get my answer i have a table with structure

 subject_level_id  | tutor_id  | level_id_fk  | subject_id_fk |
  118              | 99        | 4            | 1             |
  119              | 99        | 3            | 2             |
  120              | 99        | 3            | 3             |
  121              |100        | 3            | 1             | 
  122              |100        | 4            | 2             |

I want to extract tutor id’s of tutors which teach 1, 2 or more subjects of a particular level( number of subjects depend upon user selection of subjects there can be but level will be unique for aparticular query)

I have worked out a query with help from other posts but it is very slow and gets worse if number of subjects are more than two because I have 10,000 records of tutors my query is:

select distinct
   a.tutor_id
from
   tutor_preferred_level_subject as a
inner join
   tutor_preferred_level_subject as b
   on a.level_id_fk = b.level_id_fk
where
   a.subject_id_fk = 1 and
   b.subject_id_fk = 10 and
   a.level_id_fk = 3

Suggestion: SELECTING with multiple WHERE conditions on same column

The difference in this question and mine is that I want the tutors who teach exactly both the subjects.

update: yes main concern is performance, this query is working fine for two subjects but for more than two subjects mysql server keeps on processing forever. The index is subject_level_id and there are almost 13000 records in it and the below query returns around 6500 results.

as Jakub Sacha demanded these are the results of

EXPLAIN select distinct
   a.tutor_id
from
   tutor_preferred_level_subject as a
inner join
   tutor_preferred_level_subject as b
   on a.level_id_fk = b.level_id_fk
where
   a.subject_id_fk = 1 and
   b.subject_id_fk = 10 and
   a.level_id_fk = 3



id  select_type     table   type    possible_keys   key     key_len     ref|
1   SIMPLE          a   ALL     NULL            NULL    NULL            NULL 
1   SIMPLE          b   ALL     NULL            NULL    NULL           NULL 

rows    Extra 
12733   Using where; Using temporary
12733   Using where; Distinct
  • 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-16T18:35:55+00:00Added an answer on June 16, 2026 at 6:35 pm

    Your query does not seem to be doing what you describe. The join should be on tutor_id and not on level_id_fk.

    You need the DISTINCT only if the (level_id_fk, subject_id_fk, tutor_id) combination is not unique:

    SELECT                              
        -- DISTINCT
       a.tutor_id
    FROM
       tutor_preferred_level_subject AS a
    INNER JOIN
       tutor_preferred_level_subject AS b
           ON a.tutor_id = b.tutor_id
    WHERE
       a.subject_id_fk = 1 AND
       a.level_id_fk = 3 AND
       b.subject_id_fk = 10 AND
       b.level_id_fk = 3 ;
    

    Additionally, you should add a (unique) index on (level_id_fk, subject_id_fk, tutor_id) for efficiency:

    ALTER TABLE tutor_preferred_level_subject
      ADD INDEX level_subject_tutor_IDX
        (level_id_fk, subject_id_fk, tutor_id) ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have seen many posts about this problem but didn't get an answer. I
I have seen many answers on stackoverflow, but I didn't find an answer that
I have seen many posts on this subject, but none have been answered, and
Hi all I have seen many articles on url rewriting but I didn't find
I have seen many posts and questions about Mocking a private method but still
I have seen many posts about how to turn an array into a table,
I have seen many posts to this problem but no prescribed solution worked for
I have seen many posts about when to use ViewBag/ViewData vs ViewModel but i
I have seen many question and applied their results but nothing happens I still
I have seen many answers for this type of question but its not related

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.