I've searched on my question but I couldn't really find what I was looking for or maybe I just didn't understand the examples. If there is a similar post please point me to the right thread.
What I'm trying to do is the following: I have results like the table below which I generated with a very simple query:
SELECT id, first_name, last_name, email, roles, created
FROM user
As you can see a user can have two roles: User or teacher. Some persons are only teacher and some are only user. However, some of them are both teacher and user. Now I want to group by e-mail adres, but of course this doesn't work on persons who are both user and teacher. I would like to group by e-mail and in case a person has both roles I want to keep the user role in the results. I understood this can be done with an if condition but I can't figure out where or how to do it.
+------+------------+-----------+-----------------------+--------------+
| id | first_name | last_name | email | roles |
+------+------------+-----------+-----------------------+--------------+
| 9798 | person | one | personOne@gmail.com | ROLE_USER |
| 9800 | person | one | personOne@gmail.com | ROLE_TEACHER |
| 9801 | person | two | personTwo@gmail.com | ROLE_TEACHER |
| 9802 | person | three | personThree@gmail.com | ROLE_TEACHER |
| 9803 | person | four | personFour@gmail.com | ROLE_USER |
+------+------------+-----------+-----------------------+--------------+
So my query should be something like this:
SELECT id, first_name, last_name, email, roles, created
FROM user
group by email (if count(email) > 1 "ROLE_USER from roles should end up in results")
Could anybody point me in the right direction or make an example? Thanks so much!