I need some help with the following situation:
Very simplified table users_log
+-----------------+-------------------+
| id | account_nr | email |
+-----------------+-------------------+
| 1 | 0000001 | cust1_@mail.com |
| 2 | 0000001 | cust1_@mail.com |
| 3 | 0000002 | cust2_@mail.com |
| 4 | 0000003 | cust3_@mail.com |
| 5 | 0000002 | cust2_@mail.com |
| 6 | 0000001 |cust1_new@mail.com |
+-----------------+-------------------+
- It is allowed for customers to appear multiple times in this table.
- Customers can change their email addresses over time, old entries are not updated.
As you can see account ‘0000001’ appears 3 times and at some point changed his email address.
I need a query that only returns the results of customers that have never changed their email address. Furthermore I need all single entries, so nothing grouped.
So all entries by customers ‘0000002’ and ‘0000003’ would be the expected result of the query in the example above.
I figured out a way using mutliple loops in a scripting language but wonder if there is a more efficient query I can use hence reducing database load?
This is a very large database and I need the fastest query possible – thank you in advance!
PS: The database structure is this way and I have no way to change anything.
Result: