In my database table I have two columns that hold either 0 or 1.
I have type and Gender, where type means 0 => teacher and 1 => student and for gender: 0 => male and 1 => female.
How can I write a single sql query to get number of teachers, students, males and females?
Right now I have:
select COUNT(type) as teachers from my_table where type = 0; // Teachers
select COUNT(type) as students from my_table where type = 1; // Students
select COUNT(gender) as males from my_table where type = 0; // Males
select COUNT(gender) as females from my_table where type = 1; // Females
Can it be done in one query? If so, how?
This way you can do it in a single query. If you have only two types of data in your table then you don’t need to specify
INconditions inWHEREclause: