1

I have a database that looks like this

 leads
 name         zip          city      county     state
 Bill         84058
 Susan        90001

 FullUSZipCodes
 ZipCode       City        County       State
 84058         Orem        Utah         Utah
 90001         Los Angeles South        California

As you can see, in the leads database the city, count and state are empty, i'm trying to merge the data so that the first table would look like this:

 leads
 name         zip          city      county     state
 Bill         84058        Orem      Utah       Utah
 Susan        90001        Los Angeles South    California

This is the first query I tried:

UPDATE leads SET leads.county = FullUSZipCodes.County WHERE leads.zip = FullUSZipCodes.ZipCode

which didn't work, and here is the 2nd query:

UPDATE leads INNER JOIN FullUSZipCodes ON leads.zip = FullUSZipCodes.ZipCode SET leads.county = FullUSZipCodes.County, leads.city = FullUSZipCodes.City, leads.state = FullUSZipCodes.State

1 Answer 1

1

Your second query looks like it should work, but I would write it slightly differently:

UPDATE leads t1
INNER JOIN FullUSZipCodes t2
    ON t1.zip = t2.ZipCode
SET
    t1.city = t2.City,
    t1.county = t2.County,
    t1.state = t2.State
WHERE
    t1.city IS NULL OR t1.county IS NULL OR t1.state IS NULL;

If the leads table has a lot of records, and performance is a concern, you can try adding a WHERE clause to the update query which targets only those records which are missing one or more pieces of address information. I think there is nothing wrong with always updating all three columns together, because typically this information tends to be grouped together.

Sign up to request clarification or add additional context in comments.

4 Comments

That is the 2nd query I tried, it just kept processing the query but didn't seem to ever finish
You can also add a WHERE clause to try and target records which are missing data.
About 50,000 records, all of them are missing the data
Possibly, it’s been running the query for 20min I️ knew it would take time, didn’t realize it would take that long

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.