简体   繁体   English

使用 JSON 数组作为值时 MySQL 语法错误

[英]MySQL Syntax Error when using JSON array as value

My Table:我的表:

  • customer_id INT (PRIMARY KEY) customer_id INT(主键)
  • purchased_products JSON购买_产品 JSON
  • refunded_products = JSON已退款产品 = JSON

Expected values预期值

  • 12345 12345
  • ["32","33","34"] ["32","33","34"]
  • ["31","38","39"] ["31","38","39"]

The following SQL works as expected.以下 SQL 按预期工作。 Great!伟大的!

 -- Insert a new row into the purchased products table
INSERT INTO dc_purchased_products (
    user_id, -- INT
    purchased_products -- JSON
)

-- 1) The user ID (primary key)
-- 2) Formatted json array with the first purchased product ID
VALUES ( 12345, '["36"]' )

-- If the user id already exists, append the existing array with the product ID
ON DUPLICATE KEY UPDATE 

-- JSON_ARRAY_INSERT(existing purchases array, index, product_id)
purchased_products = JSON_ARRAY_INSERT(purchased_products, '$[0]', "36") 

However, my PDO satements in my application arent so good.但是,我的应用程序中的 PDO 语句并不是那么好。

$item = [
  'statement' => "INSERT INTO purchased_products 
                        (customer_id, purchased_products) 
                  VALUES(:customer_id, [:purchased_products]) 
                    ON DUPLICATE KEY 
                    UPDATE purchased_products = JSON_ARRAY_INSERT(purchased_products, '$[0]',:purchased_products)",
  'data' => [
    ['customer_id' => 12345, 'purchased_products' => '"36"'],
    ['customer_id' => 12345, 'purchased_products' => '"37"']
  ]
]


My Connection我的连接

$this->connection = new PDO("mysql:host=$servername;dbname=$database", $u, $p, [
  PDO::MYSQL_ATTR_SSL_KEY                => $ck,
  PDO::MYSQL_ATTR_SSL_CERT               => $cc,
  PDO::MYSQL_ATTR_SSL_CA                 => $sc,
  PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => false,
]);

$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);


$statement = $this->connection->prepare($item['statement']);
foreach ($item['data'] as $rowData) {

    foreach ($rowData as $key => $param) {
        $statement->bindValue(':' . $key, $param);
    }

    try {
        $success = $statement->execute();
    }

    catch (PDOException $e) {
        pre($e->getMessage());        
    }

}

Error message错误信息

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; SQLSTATE[42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near '['3784835']) ON DUPLICATE KEY UPDATE purchased_products = JSON_ARRAY_INSERT(purc' at line 1检查与您的 MySQL 服务器版本相对应的手册,了解在 '['3784835']) 附近使用的正确语法 ON DUPLICATE KEY UPDATE purchase_products = JSON_ARRAY_INSERT(purc' 在第 1 行

This should fix the problem:这应该可以解决问题:

$item = [
  'statement' => "INSERT INTO purchased_products 
                        (customer_id, purchased_products) 
                  VALUES(:customer_id, :purchased_products) 
                    ON DUPLICATE KEY 
                    UPDATE purchased_products = JSON_ARRAY_INSERT(purchased_products, '$[0]',:purchased_products_json)",
  'data' => [
    ['customer_id' => 12345, 'purchased_products' => '["36"]', 'purchased_products_json' => '36'],
    ['customer_id' => 12345, 'purchased_products' => '["37"]', 'purchased_products_json' => '37'],
  ]
];

Explanation of changes:变更说明:

Removed [ and ] in VALUES(:customer_id, [:purchased_products]) [ ] VALUES(:customer_id, [:purchased_products])中的 [ 和 ]

Now it looks like this:现在看起来像这样:

VALUES(:customer_id, :purchased_products)

This change will avoid the error during the execute() .此更改将避免execute()期间的错误。

New param in JSON_ARRAY_INSERT: purchased_products_json JSON_ARRAY_INSERT 中的新参数:购买的_products_json

Now it looks like this:现在看起来像这样:

JSON_ARRAY_INSERT(purchased_products, '$[0]',:purchased_products_json)

To avoid the error now we need two params with small diferences.为了避免错误,现在我们需要两个差异很小的参数。 One for the INSERT ( purchased_products ) and other for the UPDATE ( purchased_produtcs_json ) because they have different formats despite same values.一个用于插入( purchased_products的产品)和另一个用于更新( purchased_produtcs_json的产品json),因为尽管值相同,但它们具有不同的格式。

In data array, changed 'purchased_products' => '"36"'data数组中,更改了'purchased_products' => '"36"'

Now it looks like this:现在看起来像这样:

'purchased_products' => '["36"]'

because we will use it during INSERT stament, and we need to set it as new JSON array value with correct format and because we already removed the [ and ] in the previous INSERT statement.因为我们将在 INSERT 语句中使用它,我们需要将其设置为新的 JSON 数组值,格式正确,因为我们已经删除了前面 INSERT 语句中的[]

In data array, added new param: 'purchased_products_json'data数组中,添加了新参数: 'purchased_products_json'

In order to UPDATE the field, the value format should be different, so we need this new param.为了更新字段,值格式应该不同,所以我们需要这个新参数。 It looks like this:它看起来像这样:

'purchased_products_json' => '36'

avoiding the use of [ and ] .避免使用[] You can see some information about JSON_ARRAY_INSERT in mariadb documentation您可以在mariadb 文档中看到有关 JSON_ARRAY_INSERT 的一些信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM