0

我的查询只是在插入UPDATE中更新jobResponsibilities字段,但我想 在一条记录中插入所有值,例如不仅 j 。table_2jobResponsibilities = c00017jobResponsibilities = C00001,C00302,C00303,C00287,C00286,C00285,C00017obResponsibilities = C00017

我的代码如下:

function mySQLSafe( $value,  $quote = "'"){

    // strip quotes if already in
    $value = get_magic_quotes_gpc() ? stripslashes($value) : $value;
    $value = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($value) : mysql_escape_string($value);

    // Stripslashes
    if (get_magic_quotes_gpc()){
        $value = stripslashes($value);
    }

    // Quote value
    if (version_compare(phpversion(), "4.3.0") == "-1") {
        $value = mysql_escape_string($value);
    } else{
        $value = mysql_real_escape_string($value);
    }

    $value = $quote . $value . $quote;
    return $value;
}


$comcode = "SELECT  jobCode, compCode 
FROM first_table";

$comcode_RS = mysql_query($comcode, $timespace) or die (mysql_error());

while($row_comcode_RS = mysql_fetch_assoc($comcode_RS))
{
    $jobCode = $row_comcode_RS['jobCode']; // more than one record
    $compCode = $row_comcode_RS['compCode'];  // more than one record

    echo $updateSQL = "UPDATE second_table  SET jobResponsibilities=".mySQLSafe($compCode)."  WHERE jobCode=".mySQLSafe($jobCode).""; 
    $Result1 = mysql_query($updateSQL, $timespace) or die(mysql_error());
}
4

1 回答 1

0

Use CONCAT to append the new value to the end of the column.

$updateSQL = "UPDATE second_table
              SET jobResponsibilities = IF(jobResponsibilities = '', " .
                                           mSQLSafe($compCode) . ",
                                           CONCAT(jobResponsibilities, ',', " . mSQLSafe($compCode) . "))
              WHERE jobCode=".mySQLSafe($jobCode);

However, this is poor schema design. Columns with comma-separated lists make many database operations difficult. You should use a separate table for job responsibilities, with rows for each combination of jobCode and compCode.

于 2013-11-01T15:52:24.240 回答