Jump to content

Update with subquery - error returns more that one row


Recommended Posts

I have 2 tables described below. I am trying to take latitude and longitude from table B and update table A. I could just use table B but it is huge, 1Mil+ records and even though indexed, queries are slow.

 

Table A: (cityzip)

 

ID | zip_code |      city | state | latitude | longitude

------------------------------------------------------------

1  | 30281    | Atlanta | GA    |              |

...

------------------------------------------------------------

 

Table B: (ip_geolocation)

goecode_id | ip | country_code | country_name | region_name | city | postal_code | latitude | longitude

----------------------------------------------------------------------------------------------------------------------------------------------

0                | 555555555 | USA                | United States | GA                  | Atlanta | 30281 | 34.000 | -45.099

...

-----------------------------------------------------------------------------------------------------------------------------------------------

 

UPDATE  `cityzip` AS a
SET `latitude` = (SELECT DISTINCT `latitude` 
                  FROM `ip_geolocation`
                  WHERE UPPER(`city`)              = UPPER(a.`city`)
                  AND   UPPER(`region_name`) = UPPER(a.`state`))
WHERE a.`latitude` = '';

Error #1242 - Subquery returns more than 1 row

possible for the same Region_name - City maybe

example : http://www.zipcodedownload.com/~Resources/Images/USAZIP5Premium.gif

 

I will run this query to find the records

SELECT region_name, city, count(geocode_id)
   FROM `ip_geolocation`
  GROUP BY region_name, city
  HAVING count(geocode_id) > 1

DISTINCT just means you will not get any duplicate rows; it does not mean you will only get a single row. If you are getting more than one row in the subquery with a DISTINCT, then there are DIFFERENT latitude values for that City/State combination.

 

You might try doing a JOIN instead of a subquery; of course, the latitude/longitude that you get will be randomly selected from the multiple entries in the second table.

I got it working using the following. Although I have to go state by state to keep the query from timing out.

 

UPDATE  `cityzip` AS a
SET `latitude` = (SELECT DISTINCT `latitude` 
                            FROM `ip_geolocation`
                            WHERE UPPER(`city`)              = UPPER(a.`city`)
                            AND   UPPER(`region_name_short`) = UPPER(a.`state`) limit 1)
WHERE a.`state` = 'AK'
AND   a.`latitude` = '';

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.