Let us first create a table. Here, we have set the status using ENUM −
mysql> create table DemoTable2037 -> ( -> StudentId int, -> status enum('Active','Inactive') -> ); Query OK, 0 rows affected (0.51 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable2037 values(99,'Active'); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable2037 values(99,'Inactive'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable2037 values(100,'Inactive'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable2037 values(99,'Active'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable2037 values(100,'Inactive'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable2037 values(101,'Active'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable2037 values(101,'Active'); Query OK, 1 row affected (0.12 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable2037;
This will produce the following output −
+-----------+----------+ | StudentId | status | +-----------+----------+ | 99 | Active | | 99 | Inactive | | 100 | Inactive | | 99 | Active | | 100 | Inactive | | 101 | Active | | 101 | Active | +-----------+----------+ 7 rows in set (0.00 sec)
Here is the query to select records with ACTIVE status −
mysql> select StudentId,status -> from DemoTable2037 where status='Active' -> group by StudentId;
This will produce the following output −
+-----------+--------+ | StudentId | status | +-----------+--------+ | 99 | Active | | 101 | Active | +-----------+--------+ 2 rows in set (0.00 sec)