简体   繁体   中英

MySQL compare two different tables with different contraints

MySQl Comparison of Two Different Tables

I have two tables with these structures:

Table 1

FullContact
-----------
id
name
phone
address
email
phone
registration
marketingId
created

Table 2

Contacted
-----------
first_name
last_name
email

The FullContact table contains people and the Contacted might contain the same individuals but is given back to me in a strange column structure.

The FullContact might contain duplicates and the Contacted has no duplicates.

Main Goal: I want to see how many from Contacted are in FullContact.

The query I am trying to run is of this form:

SELECT distinct fc.name, fc.email, fc.phone, concat_ws(" ",c.first_name, c.last_name),
c.phone from Contacted c 
INNER JOIN FullContact fc 
ON ( concat_ws(" ", c.first_name,
c.last_name) = fc.name or c.email = fc.email or (fc.phone REGEXP '[0-9]+$') = c.phone)   
and fc.created >= '2011-11-01';

Explanation of Query: I am trying to select the items in FullContact that are the same as Contacted by using an inner join and constraining by three string parameters and a date parameter.

Problem: I made the select statement to show the results of items from FullContact and items from Contacted. The results contain FullContact columns being unique and Contact values duplicating.

Is this the correct query to run?

Thanks

You are checking for three lots of duplicates in there, name, email or phone. So if you have two different names with the same email, you get two records in the result. I would have expected and in the on clause.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM