INSERT ON DUPLICATE KEY UPDATE 语句的复杂 mysql 查询错误

Complex mysql query error with INSERT ON DUPLICATE KEY UPDATE statement

对于我正在开发的 Web 应用程序,我有一个非常复杂的 SQL 语句。哪个在工作之前。但是我不知道发生了什么变化..

sql 错误:

"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ON DUPLICATE KEY UPDATE bankaccountid = VALUES(bankaccountid), ownerid = VALUES(' at line 1"

我的查询是:

<?php
if($bank_name1 !== '') {
        $bank1 = "('$bank_id1', '$owner_id', '$ownertype1', '$accounttype1', '$currency1', '$bank_name1', '$bank_loc1', '$bank_country1', '$bank_accountno1', '$bank_sortcode1', '$bank_iban1', '$bank_bicswift1', '$secondary1'),";
    } else {
        $bank1 = '';
    }

    if($bank_name2 !== '') {
        $bank2 = "('$bank_id2', '$owner_id', '$ownertype2', '$accounttype2', '$currency2', '$bank_name2', '$bank_loc2', '$bank_country2', '$bank_accountno2', '$bank_sortcode2', '$bank_iban2', '$bank_bicswift2', ''),";
    } else {
        $bank2 = '';
    }

    if($bank_name3 !== '') {
        $bank3 = "('$bank_id3', '$owner_id', '$ownertype3', '$accounttype3', '$currency3', '$bank_name3', '$bank_loc3', '$bank_country3', '$bank_accountno3', '$bank_sortcode3', '$bank_iban3', '$bank_bicswift3', '$secondary3'),";
    } else {
        $bank3 = '';
    }

    if($bank_name4 !== '') {
        $bank4 = "('$bank_id4', '$owner_id', '$ownertype4', '$accounttype4', '$currency4', '$bank_name4', '$bank_loc4', '$bank_country4', '$bank_accountno4', '$bank_sortcode4', '$bank_iban4', '$bank_bicswift4', '')";
    } else {
        $bank4 = '';
    }

    $sql = "INSERT INTO bankaccounts (bankaccountid, ownerid, ownertype, accounttype, currency, bankname, location, bankcountry, accountno, sortcode, iban, bicswift, secondary) VALUES ".$bank1." ".$bank2." ".$bank3." ".$bank4." ON DUPLICATE KEY UPDATE bankaccountid = VALUES(bankaccountid), ownerid = VALUES(ownerid), ownertype = VALUES(ownertype), accounttype = VALUES(accounttype), currency = VALUES(currency), bankname = VALUES(bankname), location = VALUES(location), bankcountry = VALUES(bankcountry), accountno = VALUES(accountno), sortcode = VALUES(sortcode), iban = VALUES(iban), bicswift = VALUES(bicswift), secondary = VALUES(secondary)";

我已经重新阅读这个查询一个小时了。我一定是遗漏了一些相当愚蠢的东西...

这是原始 SQL 查询:

INSERT INTO bankaccounts (bankaccountid, ownerid, ownertype, accounttype, currency,
    bankname, location, bankcountry, accountno, sortcode, iban, bicswift, secondary)
    VALUES ".$bank1." ".$bank2." ".$bank3." ".$bank4."
ON DUPLICATE KEY UPDATE bankaccountid = VALUES(bankaccountid),
    ownerid = VALUES(ownerid), ownertype = VALUES(ownertype),
    accounttype = VALUES(accounttype), currency = VALUES(currency),
    bankname = VALUES(bankname), location = VALUES(location),
    bankcountry = VALUES(bankcountry), accountno = VALUES(accountno),
    sortcode = VALUES(sortcode), iban = VALUES(iban),
    bicswift = VALUES(bicswift), secondary = VALUES(secondary)

只要查询的 UPDATE 部分为真,一切都可以。但是当我尝试 INSERT 我得到 mysql.

抛出的错误

我把这个加到顶部

$bank_name1="b1";
$bank_name2="b2";
$bank_name3="b3";
$bank_name4="b4";

这个到底

echo $sql;

这是你的字符串:

INSERT INTO bankaccounts (bankaccountid, ownerid, ownertype, accounttype, currency, bankname, location, bankcountry, accountno, sortcode, iban, bicswift, secondary) 
VALUES ('', '', '', '', '', 'b1', '', '', '', '', '', '', ''), 
('', '', '', '', '', 'b2', '', '', '', '', '', '', ''), 
('', '', '', '', '', 'b3', '', '', '', '', '', '', ''), 
('', '', '', '', '', 'b4', '', '', '', '', '', '', '') 

ON DUPLICATE KEY UPDATE bankaccountid = VALUES(bankaccountid), ownerid = VALUES(ownerid), ownertype = VALUES(ownertype), 
accounttype = VALUES(accounttype), currency = VALUES(currency), bankname = VALUES(bankname), location = VALUES(location), 
bankcountry = VALUES(bankcountry), accountno = VALUES(accountno), sortcode = VALUES(sortcode), iban = VALUES(iban), 
bicswift = VALUES(bicswift), secondary = VALUES(secondary)

那是一场即将发生的爆炸。更新部分 不应 包含 VALUES() 包装器。

更新部分应该遵循以下形式:

update col1=someval1,col2=someval2, ...

感谢 Drew,我找到了以下解决方案。事后看来,这看起来和感觉起来更强大。

    if($bank_name1 !== '') {
        $sql = "INSERT INTO bankaccounts (ownerid, ownertype, accounttype, currency, bankname, location, bankcountry, accountno, sortcode, iban, bicswift, secondary) VALUES ('$owner_id', '$ownertype1', '$accounttype1', '$currency1', '$bank_name1', '$bank_loc1', '$bank_country1', '$bank_accountno1', '$bank_sortcode1', '$bank_iban1', '$bank_bicswift1', '$secondary1') ON DUPLICATE KEY UPDATE bankaccountid='$bank_id1', ownerid='$owner_id', ownertype='$ownertype1', accounttype='$accounttype1', currency='$currency1', bankname='$bank_name1', location='$bank_loc1', bankcountry='$bank_country1', accountno='$bank_accountno1', sortcode='$bank_sortcode1', iban='$bank_iban1', bicswift='$bank_bicswift1'";
        $mysqli->query($sql)or die(mysqli_error($mysqli));
    }

    if($bank_name2 !== '') {
        $sql = "INSERT INTO bankaccounts (ownerid, ownertype, accounttype, currency, bankname, location, bankcountry, accountno, sortcode, iban, bicswift) VALUES ('$owner_id', '$ownertype2', '$accounttype2', '$currency2', '$bank_name2', '$bank_loc2', '$bank_country2', '$bank_accountno2', '$bank_sortcode2', '$bank_iban2', '$bank_bicswift2') ON DUPLICATE KEY UPDATE bankaccountid='$bank_id2', ownerid='$owner_id', ownertype='$ownertype2', accounttype='$accounttype2', currency='$currency2', bankname='$bank_name2', location='$bank_loc2', bankcountry='$bank_country2', accountno='$bank_accountno2', sortcode='$bank_sortcode2', iban='$bank_iban2', bicswift='$bank_bicswift2'";
        $mysqli->query($sql)or die(mysqli_error($mysqli));
    }

    if($bank_name3 !== '') {
        $sql = "INSERT INTO bankaccounts (ownerid, ownertype, accounttype, currency, bankname, location, bankcountry, accountno, sortcode, iban, bicswift, secondary) VALUES ('$owner_id', '$ownertype3', '$accounttype3', '$currency3', '$bank_name3', '$bank_loc3', '$bank_country3', '$bank_accountno3', '$bank_sortcode3', '$bank_iban3', '$bank_bicswift3', '$secondary3') ON DUPLICATE KEY UPDATE bankaccountid='$bank_id3', ownerid='$owner_id', ownertype='$ownertype3', accounttype='$accounttype3', currency='$currency3', bankname='$bank_name3', location='$bank_loc3', bankcountry='$bank_country3', accountno='$bank_accountno3', sortcode='$bank_sortcode3', iban='$bank_iban3', bicswift='$bank_bicswift3'";
        $mysqli->query($sql)or die(mysqli_error($mysqli));
    }

    if($bank_name4 !== '') {
        $sql = "INSERT INTO bankaccounts (ownerid, ownertype, accounttype, currency, bankname, location, bankcountry, accountno, sortcode, iban, bicswift) VALUES ('$owner_id', '$ownertype4', '$accounttype4', '$currency4', '$bank_name4', '$bank_loc4', '$bank_country4', '$bank_accountno4', '$bank_sortcode4', '$bank_iban4', '$bank_bicswift4') ON DUPLICATE KEY UPDATE bankaccountid='$bank_id4', ownerid='$owner_id', ownertype='$ownertype4', accounttype='$accounttype4', currency='$currency4', bankname='$bank_name4', location='$bank_loc4', bankcountry='$bank_country4', accountno='$bank_accountno4', sortcode='$bank_sortcode4', iban='$bank_iban4', bicswift='$bank_bicswift4'";
        $mysqli->query($sql)or die(mysqli_error($mysqli));
    }

所以每个银行都有自己的条件查询。