I’ve been googling this for a while now but found no solution. OK I have 2 tables that look like this:
Table "main":
client_email | client_name |
---|---|
[email protected] | Peter Pan |
[email protected] | Paul Smith |
Table "registered":
client_email | client_name |
---|---|
[email protected] | James Bond |
[email protected] | Paul Smith |
Now some clients from table "main" will be in table "registered" but not all of them. Also there may be some clients who are in table "registered" but not in table "main". And lastly, the same client (email) may be multiple times in table main but only once in table registered.
Now what I want to do is a query that finds me all UNIQUE emails from both tables and the corresponding names (I don’t care from which table).
I have tried:
SELECT client_email,client_name
FROM `main`
UNION
SELECT client_email,client_name
FROM `registered`
ORDER BY client_email ASC;
but this yielded duplicate emails as it not just looks for unique emails but entries where BOTH the email and client name are unique. So having the client name spelled just slightly different in one entry (e.g., with a hyphen or accent, etc.) will result in that email being collected more than once. I can get it to work on a single table using the GROUP BY(client_email) but I can’t figure out how to collect all unique emails from 2 or more tables.
2
Answers
perhaps wrap your query and group_concat name https://dbfiddle.uk/DSWlTERj
The sub query will dedupe on email and name and the outer query will list all the names associated with an email.
You can group by client_email and take any client_name, if there several clients with this email.
Possible use UNION … segment of subquery several times, how many tables do you have.
See example
If you can see client_name doubles for email, add column count().
And HAVING count()>1 for filter doubles.