SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Guru
    Join Date
    Sep 2008
    Posts
    977
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    update with sub query troubling me.

    trying to update records in one table, with values from another because I have added a new column.

    I need to take the two char country code from one table and put it into another with an update statement. The rows in which I input this new value, must contain the corresponding three char country code.

    the three char country code is in both tables so shouldn't I be joining them, to ensure the two char code goes into the correct rows?

    this query below fails because the sub query brings in more than one row.

    Code MySQL:
    update country_codes
    set two_char_country_code = (
    select distinct
    ip.two_char_country_code
    , ip.three_char_country_code
    from ip_to_country AS ip
    where country_codes.three_char_country_code = ip.three_char_country_code

    sorry for being so quick to have gone rusty.

    bazz

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,215
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    is this what you wanted?
    Code:
    UPDATE country_codes
    INNER
      JOIN ip_to_country 
        ON ip_to_country.three_char_country_code = country_codes.three_char_country_code 
       SET country_codes.two_char_country_code = ip_to_country.two_char_country_code
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •