This has been asked but I dont quite match existing questions with my case.
I have two tables, the first table is credentials with id username and email
and an email-alias table with user-id (which corresponds to credentials.id) and email. Emails in credentials are more often “user.name@domain.com” while in alias they’d be “usern@domain.com”.
All I have now is
SELECT `username` FROM `credentials` WHERE `email` LIKE ?
But the email will not always match if I query with “usern@domain.com”. What I want to do is get the username with one query which would fall back to email-alias and use “usern@domain.com” to get an user-id from there to be used again in credentials to match a username
The pitfall is that the supplied email could be either an aliased one “usern@..” or “user.name@..”
mysql> describe `email-alias`;
+---------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| user-id | int(11) | NO | UNI | NULL | |
| email | varchar(100) | YES | | NULL | |
+---------+--------------+------+-----+---------+----------------+
mysql> describe `credentials`;
+-----------------+--------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------------------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| username | varchar(100) | NO | UNI | NULL | |
| email | varchar(100) | NO | | NULL | |
+-----------------+--------------+------+-----+---------------------+----------------+
Your question is kind of confusing but I think what you’re trying to do is select
usernamefrom the first table if theemailexists and if it doesn’t select from the 2nd table if it exists. You would use subqueries for that. Hope this helps.