Update using a specific record

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jkwok
    New Member
    • Feb 2008
    • 32

    Update using a specific record

    Hi,

    I have a table called 'Countries' with country codes, and other related attributes.

    Countries
    id country_code full_name rate

    I have another table, 'Consumption', that links to the Countries table by the attribute 'Country'. Country links to 'full_name'. (Not my design!)

    I want to update an attribute for every record in Consumption when 'Country' has no match with 'full_name'. So for example, if Country is blank, or has a value that isn't in the Countries table.

    If this should occur, I want to be able to assign a specific 'full_name' within the Countries table. I've made a row specifically for this scenario. I've been trying to use an update query but I've had no results so far.

    Countries
    34 - NO - No Site ID - $79.99

    Any help would be greatly appreciated!

    Thanks,
    Jason
  • JKing
    Recognized Expert Top Contributor
    • Jun 2007
    • 1206

    #2
    Hi Jason,

    Here's some SQL that I think should satisfy your needs.

    [code=sql]
    UPDATE consumption SET consumption.cou ntry = 'Site ID'
    WHERE NOT Exists (SELECT countries.full_ name FROM countries WHERE countries.full_ name = consumption.cou ntry);
    [/code]

    I think I got all the field names correct from your description, so you should be able to copy and paste without any problems.

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32663

      #3
      Another way, more typical of what Access might produce for you within the design window, would be :
      [code=sql]UPDATE [Consumption] AS tC LEFT JOIN [Countries] AS tCs
      ON tC.Country=tCs. Full_Name
      SET tC.Country='No Site ID'
      WHERE tCs.Full_Name Is Null[/code]

      Comment

      • jkwok
        New Member
        • Feb 2008
        • 32

        #4
        Thanks guys, both responses are really helpful!

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32663

          #5
          No worries. It's good to help :)

          Comment

          Working...