If that is how you want the output then GROUP BY may well be the answer
student data...
+-------+-----------+----------+
| regno | firstname | lastname |
+-------+-----------+----------+
| 9738 | Jane | Jenkins |
| 9844 | Janet | Gordon |
| 9966 | Liz | Lyle |
| 9978 | Olivia | Unwin |
| 9979 | Curly | NULL |
| 9980 | NULL | Larry |
| 9981 | NULL | Mo |
| 9982 | Fred | |
| 9983 | Emily | NULL |
+-------+-----------+----------+
query...
SELECT CASE WHEN IFNULL(firstname, '') = '' THEN 'No first name'
WHEN IFNULL(lastname, '') = '' THEN 'No last name'
ELSE 'OK'
END as category
, COUNT(*) as Errors
, GROUP_CONCAT(regno SEPARATOR ', ') as IDs
FROM student
GROUP BY category
HAVING category <> 'OK';
results...
+---------------+--------+------------------+
| category | Errors | IDs |
+---------------+--------+------------------+
| No first name | 2 | 9980, 9981 |
| No last name | 3 | 9983, 9979, 9982 |
+---------------+--------+------------------+
CAUTION: the default maximum length of GROUP_CONCAT item is 1024 characters so in my example I would only get a max of 170 ids listed.