Marklitz has asked for the wisdom of the Perl Monks concerning the following question:

Hi everybody, I want to compare 2 tables contents. My tables are:
Table1 ------------------ ID || Value ------------------ 1 || forum_list1 2 || Message_list1 3 || Search1 4 || Search5 5 || Search11 6 || Log_out 7 || LOG
Table2 -------------------------------------------- ID || Value1 || Value2 --------------------------------------------- 1 || rd_forum_list1_no || 2 || wr_Message_list1_yes || 3 || rd_Search1_no || 4 || rd_Search5_no || 5 || rd_Search11_no || 6 || rd_Log_out_no || 7 || rd_LOG_no || 8 || wr_forum_list1_no || 9 || rd_Message_list1_yes || 10 || 123_Search1_456 || 11 || 123_Search11_456 ||
I want to compare both the tables content and if the contents are similar then place the ID of table no. one in Table2.Value2 but discard all the rows with "wr_". Also note that in case of Search1 and Search11, I want the comaprison very exact as
Table2 -------------------------------------------- ID || Value1 || Value2 --------------------------------------------- 1 || rd_forum_list1_no || 1 2 || wr_Message_list1_yes || 3 || rd_Search1_no || 3 4 || rd_Search5_no || 4 5 || rd_Search11_no || 5 6 || rd_Log_out_no || 6 7 || rd_LOG_no || 7 8 || wr_forum_list1_no || 9 || rd_Message_list1_yes || 2 10 || 123_Search1_456 || 3 11 || 123_Search11_456 || 5
I tried implementing this by following code:
$query = "SELECT Table1.value, Table2.value1 FROM Table1,Table2 WHERE +Table2.value1 LIKE CONCAT('%',Table1.value,'%')"; $result =mysql_query($query) or die(mysql_error()); while(@row = mysql_ftech_array($result)) { $updatetable2= UPDATE Table2.value2 SET Table2.value2 =(@Table1.value) +; }
Seems it works fine till $result but show error for updatetable2. It shows an error for @Table1.value. \n But its not working.. please help.. thanks in advance

Replies are listed 'Best First'.
Re: Perl and Mysql compare 2 tables
by zwon (Abbot) on Jun 25, 2009 at 18:22 UTC

    This should be quoted like that:

    $updatetable2= q{UPDATE Table2.value2 SET Table2.value2 =(@Table1.valu +e)};
Re: Perl and Mysql compare 2 tables
by mzedeler (Pilgrim) on Jun 26, 2009 at 07:11 UTC

    The query looks okay, but where did you get the mysql_query from? It looks suspiciously much like PHP. If you are used to PHP, you should get aquainted with DBI which is the major perl-way of talking to databases.

    Also, even if you may be able to use the mysql_-prefixed functions (using some odd perl module that I am not aware of), I am sure it shouldn't read mysql_ftech_array.

    Finally, assigning string values require that you quote them:

    $updatetable2= UPDATE Table2.value2 SET Table2.value2 =(@Table1.value)

    Probably should read:

    $updatetable2 = "UPDATE Table2.value2 SET Table2.value2 =(" . $row[0] +.")";

    (@Table1.value is a mix of perl and JavaScript syntax and wouldn't work in either one of them.)

    Consider spending a little time looking at the manual page for DBI and see how you can access databases there.

Re: perl and mysql- compare 2 tabels.
by jbt (Chaplain) on Jun 29, 2009 at 12:58 UTC
    UPDATE syntax in different from SELECT syntax.
    Try something like
    ...
    UPDATE Table2 SET value2 =
    ...

    jbt