Updating a coloumn value by comparing coloumns from two tables

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dhillarun
    New Member
    • Mar 2007
    • 18

    Updating a coloumn value by comparing coloumns from two tables

    Hi all,

    I need to update field three of first table with field three of second
    tables for all records that match field one of first table with field
    one of second table (both are primary key for each table and are same).

    I need to write an ANSI sql query to do this job.

    I tried follwing , but it didn't work.

    update tab1 set t1.c=t2.c from tab1 t1,tab2 t2 where t1.a=t2.a;

    testdb=# \d tab1
    Table "public.tab 1"
    Column | Type | Modifiers
    --------+---------+-----------
    a | integer |
    b | integer |
    c | integer |

    testdb=# \d tab3
    Table "public.tab 3"
    Column | Type | Modifiers
    --------+---------+-----------
    a | integer |
    b | integer |
    c | integer |

    Pl. do reply me.
  • rski
    Recognized Expert Contributor
    • Dec 2006
    • 700

    #2
    Originally posted by dhillarun
    Hi all,

    I need to update field three of first table with field three of second
    tables for all records that match field one of first table with field
    one of second table (both are primary key for each table and are same).

    I need to write an ANSI sql query to do this job.

    I tried follwing , but it didn't work.

    update tab1 set t1.c=t2.c from tab1 t1,tab2 t2 where t1.a=t2.a;

    testdb=# \d tab1
    Table "public.tab 1"
    Column | Type | Modifiers
    --------+---------+-----------
    a | integer |
    b | integer |
    c | integer |

    testdb=# \d tab3
    Table "public.tab 3"
    Column | Type | Modifiers
    --------+---------+-----------
    a | integer |
    b | integer |
    c | integer |

    Pl. do reply me.
    It should works

    update tab1 set tab1.c=tab2.c from tab2 where tab1.a=tab2.a;

    Comment

    • dhillarun
      New Member
      • Mar 2007
      • 18

      #3
      Originally posted by rski
      It should works

      update tab1 set tab1.c=tab2.c from tab2 where tab1.a=tab2.a;
      Sorry I am getting following error.

      testdb=# update tab1 set tab1.c=tab2.c from tab2 where tab1.a=tab2.a;
      ERROR: syntax error at or near "." at character 21

      Comment

      • amitpatel66
        Recognized Expert Top Contributor
        • Mar 2007
        • 2358

        #4
        Originally posted by dhillarun
        Hi all,

        I need to update field three of first table with field three of second
        tables for all records that match field one of first table with field
        one of second table (both are primary key for each table and are same).

        I need to write an ANSI sql query to do this job.

        I tried follwing , but it didn't work.

        update tab1 set t1.c=t2.c from tab1 t1,tab2 t2 where t1.a=t2.a;

        testdb=# \d tab1
        Table "public.tab 1"
        Column | Type | Modifiers
        --------+---------+-----------
        a | integer |
        b | integer |
        c | integer |

        testdb=# \d tab3
        Table "public.tab 3"
        Column | Type | Modifiers
        --------+---------+-----------
        a | integer |
        b | integer |
        c | integer |

        Pl. do reply me.
        You have used tab2 in the query, but from the above table structure, I see table name as tab3

        Comment

        • dhillarun
          New Member
          • Mar 2007
          • 18

          #5
          Originally posted by amitpatel66
          You have used tab2 in the query, but from the above table structure, I see table name as tab3
          Actually I need to update one table value into another table value.
          Sry, I made that mistake . it should be 'tab2' only.

          testdb=# \d tab2
          Table "public.tab 2"
          Column | Type | Modifiers
          --------+---------+-----------
          a | integer |
          b | integer |
          c | integer |

          Comment

          • rski
            Recognized Expert Contributor
            • Dec 2006
            • 700

            #6
            Originally posted by dhillarun
            Sorry I am getting following error.

            testdb=# update tab1 set tab1.c=tab2.c from tab2 where tab1.a=tab2.a;
            ERROR: syntax error at or near "." at character 21
            Sorry my mistake
            update tab1 set c=tab2.c from tab2 where tab1.a=tab2.a;

            Comment

            • dhillarun
              New Member
              • Mar 2007
              • 18

              #7
              Originally posted by rski
              Sorry my mistake
              update tab1 set c=tab2.c from tab2 where tab1.a=tab2.a;
              Thanks ... It's working now.

              Comment

              Working...