how to get count based on enum status using self join
I have a table called “student” in which I have used enum for status 0,1,2 where 0 for inactive, 1 for active and 2 for deleted.
that is
studentid studentenrollid studentname studentstatus
1 3 xyz 1
2 3 xyz 2
3 8 asda 1
4 7 sdd 1
5 9 asds 0
6 3 xyz 1
I need get the count based on status where studentrollid is unique.
studentrollid | inactive | active | deleted
3 0 2 1
8 0 1 0
7 0 1 0
9 1 0 0
Try this please:
SQLFIDDLE DEMO
Another shorter query:
MYSQL 5.1.61 version query
Based on OP’s create statment/insert statements given in the comments:
SQLFIDDEL DEMO 2