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
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.
(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…
You can then pick up all the student details by putting this in a sub-query…
EDIT
I don’t understand your comment. But an alternative (and potentially more generalisable) HAVING clause could be..