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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:43:38+00:00 2026-06-12T12:43:38+00:00

Now I have to create a query for search page depends on database that

  • 0

Now I have to create a query for search page depends on database that have 30 tables:

I will describe my requirement with an example:

there is a relation between table A and table B -> one to many relation

If we consider that table A is for Students info and table B to save the courses for that student each year . it is only allowed to take 3 subjects each year so the structure will be as the following:

Table A: ID ,Student-Name ,Tel#,Birthdate ……

Table B: :ID,StudentID,Date,Subject-one,Subject-two,Subject-three

so the student can take any 3 subjects per year
I want to make a query for subject called X to get all students who take it at any year but only once

example for valid student:

  StudentID  Sub1  Sub2  Sub3  Year  
  60         Z     (X)    Y      1
  60         L      W     V      2
  60         M      P     Y      3

example for invalid student:

  StudentID  Sub1  Sub2  Sub3  Year  
  10         Z     (X)    O      1
  10         L      W     V      2
  10         O      P    (x)     3

i hope that to be clear enough for my problem

  • 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-12T12:43:39+00:00Added an answer on June 12, 2026 at 12:43 pm

    I agree with comments that you would be better with one row per subject in table 2 (a normalised structure is virtually always better and more SQL friendly).

    That said, you can solve it in your structure.

    SELECT
      studentID
    FROM
      table2
    WHERE
         sub1 = 'x'
      OR sub2 = 'x'
      OR sub3 = 'x'
    GROUP BY
      studentID
    HAVING
      COUNT(*) = 1
    

    (This assumes the student can’t take the same subject twice in the same year.)

    Some varieties of SQL may allow the WHERE clause to be shortened…

    WHERE
      'x' IN (sub1, sub2, sub3)
    

    You can then pick up all the student details by putting this in a sub-query…

    SELECT
      *
    FROM
      table1
    INNER JOIN
    (
      <put the above query here>
    )
      AS lookup
        ON lookup.studentID = table1.studentID
    

    EDIT

    I don’t understand your comment. But an alternative (and potentially more generalisable) HAVING clause could be..

    HAVING
      SUM(CASE WHEN sub1 = 'x' THEN 1 ELSE 0 END +
          CASE WHEN sub2 = 'x' THEN 1 ELSE 0 END +
          CASE WHEN sub3 = 'x' THEN 1 ELSE 0 END)
      = 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a faster query, right now i have large databases. My
I have create my own NSOpenGLView class, right now the data that i want
So right now I have a simple canvas element with functions that create random
I have been trying for almost a week now to create an SQLite database
I had a requirement to create a query in SQL Server where the search condition
I have a table in MS Access database and need to create a query
I want to do a search across 2 tables that have a many-to-one relationship,
Hi I have a search results page which returns queries form the database using
I've been useing VS2005 to create sql reports and now have to upgrade to
I am currently working in this problem for hours now. I have to create

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.