If this is a query you do often, I recommend optimizing your tables to handle this.
I suggest adding an emailId
column to the users
table. When a user changes their email address, or sets an older email address as the primary email address, update the user's row in the users
table to indicate the current emailId
Once you modify your code to do this update, you can go back and update your older data to set emailId
for all users.
Alternatively, you can add an email
column to the users
table, so you don't have to do a join to get a user's current email address.