I have a table called terms
with id, name, school_id and klass_id
as its attributes. I want to return records that satisfies one of the condition. Before I explain the problem in detail, take a look at the table output:
id name school_id klass_id
---------------------------------------
1 Term1 NULL NULL
2 Term2 NULL NULL
3 Term1 1 4
4 Term2 1 4
5 Term1 1 3
6 Term2 1 3
7 Term1 1 NULL
8 Term2 1 NULL
9 Term1 7 NULL
10 Term2 7 NULL
12 Term1 7 103
13 Term2 7 103
14 term3 7 103
15 Term1 7 30
16 Term2 7 30
17 Term1 7 32
18 Term2 7 32
Now, notice that there are three kinds of scenarios here between klass_id
and school_id
- Both
klass_id
andschool_id
is notNULL
(condition 1) - Only
klass_id
isNULL
but notschool_id
(condition 2) - Both
school_id
andklass_id
isNULL
(condition 3)
I'd like to return only those records that satisfies the first condition; if it doesn't, return only those records that satisfies the second condition; if it doesn't return only those records that satisfies the third condition. How do I accomplish this in simple database query?