Jump to content

Update with subquery - error returns more that one row


mo

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Thanks for all the feedback. I actually downloaded a free zip code list and don't want to pay for a list just to have the lat and lon. I will try a join and if this does not work, I will copy table 2 and remove unneeded columns.

Link to comment
Share on other sites

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` = '';

Link to comment
Share on other sites

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.