# Miscellaneous > Structured Query Language (SQL) >  Update table using a complex select.

## ejo1

Ok, I'm confused how to pull this off. Let me describe what I have going on...

Actual database is MySQL 5.0.33

I've got this table(calls). Let's just say there are only two columns; number, location.
And I've got this other table(locations), two columns; number_pattern, name.

[calls]
16125551212,NULL
12185551212,NULL
16023571111,NULL

[locations]
1,NorthAmerica
1612,Minneapolis
16125551212,DirectoryAssistance
1602357,Phoenix
1602,Arizona

Of course in the real world I have hundreds of thousands of locations with several columns and I have millions of call records where I want to update the table and insert the "locations.name" into the location column of the calls table.

This code here does a nice job of finding the location that matches most closely.

//WORKING - one row at a time
SET @digits := "16125551212";
SELECT @digits, locations.name, FROM locations
WHERE locations.number_pattern in (
SUBSTRING(@digits,1,11),
SUBSTRING(@digits,1,10),
SUBSTRING(@digits,1,9),
SUBSTRING(@digits,1,8),
SUBSTRING(@digits,1,7),
SUBSTRING(@digits,1,6),
SUBSTRING(@digits,1,5),
SUBSTRING(@digits,1,4),
SUBSTRING(@digits,1,3),
SUBSTRING(@digits,1,2),
SUBSTRING(@digits,1,1)
)
ORDER BY LENGTH(locations.number_pattern) DESC LIMIT 1


So where I'm lost is how to write my update query to ask SQL to loop over all the records WHERE call.location = 'NULL' and do my little query to update things.

----------

