1

次の値を持つ入力行があります (例として、2 行のみを表示しますが、さらに多くの行が存在する可能性があります。もちろん、値は異なる場合があります)

       entry_id | transaction_partner_name | registration_number
values
          10             name 1                   number 1
          11             name 2                   number 2

mysql テーブルを値で更新する必要があります

クエリは次のようになります

 UPDATE 2_1_transactionpartners SET 
 CompanyName = CASE NumberRenamed 
 WHEN ? THEN ? WHEN ? THEN ? 
 END, 
 RegistrationNumber = CASE NumberRenamed 
 WHEN ? THEN ? WHEN ? THEN ? 
 END 
 WHERE NumberRenamed in (?,?)

また

$sql = "
    UPDATE 2_1_transactionpartners SET
    CompanyName = CASE NumberRenamed
    $when_then
    END, 
    RegistrationNumber = CASE NumberRenamed
    $when_then
    END
    WHERE NumberRenamed in ($placeholders_for_number_renamed)";

foreach内で変数を作成します

$entry_id_for_number_renamed = array();

foreach ($_POST['entry_id'] as $i => $entry_id) {

    $for_company_name .= ( $_POST['entry_id'][$i]. ','. $_POST['transaction_partner_name'][$i]. ',' );

    $for_registration_number .= ( $_POST['entry_id'][$i]. ','. $_POST['registration_number'][$i]. ',' );

    $entry_id_for_number_renamed[] = $_POST['entry_id'][$i];

    $when_then .= 'WHEN ? THEN ? ';
    $placeholders_for_number_renamed .= '?,';

}

$for_company_name = rtrim($for_company_name, ',');
$for_company_name = explode(',', $for_company_name);

$for_registration_number = rtrim($for_registration_number, ',');
$for_registration_number = explode(',', $for_registration_number);

$insertData = array_merge( $for_company_name, $for_registration_number, $entry_id_for_number_renamed );

より良い(より短い)解決策についてのアイデアがあれば、アドバイスをお願いします

4

1 に答える 1

1
UPDATE transactionpartners
SET transaction_partner_name = q.transaction_partner_name,
    registration_number      = q.registration_number
FROM (
    SELECT 10          AS entry_id                ,
        'new name 1'   AS transaction_partner_name,
        'new number 2' AS registration_number

    UNION

    SELECT 11          AS entry_id                ,
        'new name 3'   AS transaction_partner_name,
        'new number 4' AS registration_number
    )
    q
WHERE q.entry_id = transactionpartners.entry_id
于 2013-10-02T08:49:53.133 に答える