• <i id='1xBeY'><tr id='1xBeY'><dt id='1xBeY'><q id='1xBeY'><span id='1xBeY'><b id='1xBeY'><form id='1xBeY'><ins id='1xBeY'></ins><ul id='1xBeY'></ul><sub id='1xBeY'></sub></form><legend id='1xBeY'></legend><bdo id='1xBeY'><pre id='1xBeY'><center id='1xBeY'></center></pre></bdo></b><th id='1xBeY'></th></span></q></dt></tr></i><div id='1xBeY'><tfoot id='1xBeY'></tfoot><dl id='1xBeY'><fieldset id='1xBeY'></fieldset></dl></div>
      <bdo id='1xBeY'></bdo><ul id='1xBeY'></ul>

    <small id='1xBeY'></small><noframes id='1xBeY'>

    1. <legend id='1xBeY'><style id='1xBeY'><dir id='1xBeY'><q id='1xBeY'></q></dir></style></legend>
      <tfoot id='1xBeY'></tfoot>

        我可以在使用 SET 选项的 INSERT 查询中使用 ON DUPLICATE KEY UPDATE 吗?

        Can I use ON DUPLICATE KEY UPDATE with an INSERT query using the SET option?(我可以在使用 SET 选项的 INSERT 查询中使用 ON DUPLICATE KEY UPDATE 吗?)

          <legend id='VS5GQ'><style id='VS5GQ'><dir id='VS5GQ'><q id='VS5GQ'></q></dir></style></legend>

              <tbody id='VS5GQ'></tbody>
                <bdo id='VS5GQ'></bdo><ul id='VS5GQ'></ul>
                • <i id='VS5GQ'><tr id='VS5GQ'><dt id='VS5GQ'><q id='VS5GQ'><span id='VS5GQ'><b id='VS5GQ'><form id='VS5GQ'><ins id='VS5GQ'></ins><ul id='VS5GQ'></ul><sub id='VS5GQ'></sub></form><legend id='VS5GQ'></legend><bdo id='VS5GQ'><pre id='VS5GQ'><center id='VS5GQ'></center></pre></bdo></b><th id='VS5GQ'></th></span></q></dt></tr></i><div id='VS5GQ'><tfoot id='VS5GQ'></tfoot><dl id='VS5GQ'><fieldset id='VS5GQ'></fieldset></dl></div>

                  <small id='VS5GQ'></small><noframes id='VS5GQ'>

                  <tfoot id='VS5GQ'></tfoot>
                  本文介绍了我可以在使用 SET 选项的 INSERT 查询中使用 ON DUPLICATE KEY UPDATE 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我看到了以下内容(使用 VALUES 选项):

                  I've seen the following (using the VALUES option):

                  $query = "INSERT INTO $table (column-1, column-2, column-3) VALUES ('value-1', 'value-2', 'value-3') ON DUPLICATE KEY UPDATE SET column1 = value1, column2 = value2, column3 = value3, ID=LAST_INSERT_ID(ID)"; 
                  

                  ...但我不知道如何将 ON DUPLICATE KEY UPDATE 添加到我正在使用的内容中:

                  ... but I can't figure how to add ON DUPLICATE KEY UPDATE to what I'm using:

                  $query = "INSERT INTO $table SET
                      column-1 ='value-1',
                      column-2 ='value-2',
                      column-3 ='value-3'
                  ";
                  

                  例如:,伪代码

                  $query = "INSERT INTO $table SET
                      column-1 ='value-1',
                      column-2 ='value-2',
                      column-3 ='value-3'
                      ON DUPLICATE KEY UPDATE SET
                      column1 = value1,
                      column2 = value2,
                      column3 = value3,
                      $id=LAST_INSERT_ID(id)"; 
                      $my_id = mysql_insert_id();
                  ";
                  

                  我会发现后者更容易阅读.希望澄清一下,在手册中没有找到示例.

                  I would find the latter easier to read. Would appreciate clarification, didn't find an example in the manual.

                  干杯

                  推荐答案

                  我经常使用 ON DUPLICATE KEY UPDATE.在某些情况下,真正值得使用的是非标准 SQL 扩展.

                  I've used ON DUPLICATE KEY UPDATE a lot. For some situations it's non-standard SQL extension that's really worth using.

                  首先,您需要确保有一个唯一的键约束.ON DUPLICATE KEY UPDATE 函数仅在存在唯一密钥违规的情况下才会启动.

                  First, you need to make sure you have a unique key constraint in place. The ON DUPLICATE KEY UPDATE function only kicks in if there would've been a unique key violation.

                  这是一种常用的格式:

                   $query = "INSERT INTO $table (column1, column2, column3)
                   VALUES ('value-1', 'value-2', 'value-3')
                   ON DUPLICATE KEY UPDATE
                   column1 = values(column1),
                   column2 = values(column2),
                   column3 = values(column3);"
                  

                  column1 = values(column1) 表示如果查询没有遇到重复键违规,则使用将插入的值更新 column1."换句话说,它只是意味着将 column1 更新为插入工作时的样子.

                  column1 = values(column1) means "Update column1 with the value that would have been inserted if the query hadn't hit the duplicate key violation." In other words, it just means update column1 to what it would've been had the insert worked.

                  查看此代码,您要更新您尝试插入的所有三个列似乎并不正确.哪一列有唯一的约束?

                  Looking at this code, it doesn't seem correct that you're updating all three of the columns you're trying to insert. Which of the columns has a unique constraint on it?

                  根据 OP 的问题,根据 mysql insert 语句的SET"格式进行修改.

                  Modify based on 'SET' format of mysql insert statement per the question from the OP.

                  基本上要使用ON DUPLICATE KEY UPDATE,您只需像往常一样编写插入语句,但在末尾添加ON DUPLICATE KEY UPDATE 子句.我相信它应该像这样工作:

                  Basically to use ON DUPLICATE KEY UPDATE, you just write the insert statement as you normally would, but add the ON DUPLICATE KEY UPDATE clause tacked onto the end. I believe it should work like this:

                  INSERT INTO $table 
                      set column1 = 'value-1',
                          column2 = 'value-2',
                          column3 = 'value-3'
                  ON DUPLICATE KEY UPDATE
                      column1 = values(column1),
                      column2 = values(column2),
                      column3 = values(column3);
                  

                  同样,您要插入的列之一必须具有唯一索引(或列的组合).这可能是因为其中一个是主键,也可能是因为表上有唯一索引.

                  Again, one of the columns you're inserting has to have a unique index (or a combination of the columns). That can be because one of them is the primary key or because there is a unique index on the table.

                  这篇关于我可以在使用 SET 选项的 INSERT 查询中使用 ON DUPLICATE KEY UPDATE 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  PHP Upload File Validation(PHP 上传文件验证)
                  PHP Error - Uploading a file(PHP 错误 - 上传文件)
                  How can I write tests for file upload in PHP?(如何在 PHP 中编写文件上传测试?)
                  php resizing image on upload rotates the image when i don#39;t want it to(php在上传时调整图像大小会在我不想要它时旋转图像)
                  How to send additional data using PLupload?(如何使用 PLupload 发送附加数据?)
                  change button text in js/ajax after mp4 =gt;mp3 conversion in php(在 php 中的 mp4 =gt;mp3 转换后更改 js/ajax 中的按钮文本)

                  1. <tfoot id='ceS3z'></tfoot>

                            <tbody id='ceS3z'></tbody>
                          <legend id='ceS3z'><style id='ceS3z'><dir id='ceS3z'><q id='ceS3z'></q></dir></style></legend>
                          <i id='ceS3z'><tr id='ceS3z'><dt id='ceS3z'><q id='ceS3z'><span id='ceS3z'><b id='ceS3z'><form id='ceS3z'><ins id='ceS3z'></ins><ul id='ceS3z'></ul><sub id='ceS3z'></sub></form><legend id='ceS3z'></legend><bdo id='ceS3z'><pre id='ceS3z'><center id='ceS3z'></center></pre></bdo></b><th id='ceS3z'></th></span></q></dt></tr></i><div id='ceS3z'><tfoot id='ceS3z'></tfoot><dl id='ceS3z'><fieldset id='ceS3z'></fieldset></dl></div>

                            <bdo id='ceS3z'></bdo><ul id='ceS3z'></ul>

                            <small id='ceS3z'></small><noframes id='ceS3z'>