iks_in iks_in - 3 months ago 7
MySQL Question

Compare one table with another in mysql and display matched record

I have two mysql tabales.

Table1:opened_datatable
Table2:unidata


Table1 has only one column:
Emails

Table2 has 45 columns, three of them are:
Email_Office
,
Email_Personal1
,
Email_Personal2


I want to fetch full rows from
Table2-unidata
if
Emails
column of
Table1
matches with either
Email_Office
or
Email_Personal1
or
Email_Personal2
. I am getting little bit confused here.I tried this way:

select a.emails
from opened_datatable a
where a.Emails in (select *
from unidata b
where b.email_office=a.emails
or b.Email_Personal1=a.emails
or b.Email_Personal2=a.Emails
)


Its showing only one row of first table while I want to show matched rows of
Table2 -unidata
. First I need to mention table 2 and then I should have to match it with table 1-
opened_datatable
. But how can I do that?

Answer

Try This:

SELECT a.emails, b.* 
FROM opened_datatable a
INNER JOIN unidata b ON a.emails IN (b.email_office, b.Email_Personal1, b.Email_Personal2)