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

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

        如果行存在,则更新 MySQL 表的最快方法,否则插入.超过 2 个非唯一键

        时间:2024-08-22
          <legend id='Z3FG3'><style id='Z3FG3'><dir id='Z3FG3'><q id='Z3FG3'></q></dir></style></legend>

            • <tfoot id='Z3FG3'></tfoot>

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

                <tbody id='Z3FG3'></tbody>

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

                <bdo id='Z3FG3'></bdo><ul id='Z3FG3'></ul>
                  本文介绍了如果行存在,则更新 MySQL 表的最快方法,否则插入.超过 2 个非唯一键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我的表结构如下:

                   CREATE TABLE IF NOT EXISTS `reports` (
                    `id` int(11) NOT NULL AUTO_INCREMENT,
                    `day` int(11) NOT NULL,
                    `uid` int(11) NOT NULL,
                    `siteid` int(11) NOT NULL,
                    `cid` int(3) NOT NULL,
                    `visits` int(11) NOT NULL,
                    PRIMARY KEY (`id`)
                   )
                  

                  目前我检查 &使用以下代码段插入/更新:

                  Currently i check & insert/update with the following snippet:

                   $checkq = mysql_query("SELECT count(*) as rowexist FROM reports WHERE day='$day' AND uid='$uid' AND siteid='$sid' AND cid='$cid'") or die(mysql_error()); 
                  $checkr = mysql_fetch_array($checkq);
                  
                  if ($checkr['rowexist'] > 0) {
                   mysql_query("UPDATE reports_adv SET visits=visits+1 WHERE  day='$day' AND uid='$uid' AND siteid='$sid' AND cid='$cid'"); 
                  } else {
                   mysql_query("INSERT INTO reports_adv SET day='$day', uid='$uid', siteid='$sid', cid='$cid', visits='1'");
                  }
                  

                  如果行存在,否则使用超过 2 个非唯一键插入是否是更新此 MySQL 表的最快方法?

                  Is a fastest way to update this MySQL table if row exists else insert with more than 2 non-unique keys?

                  推荐答案

                  只需使用 INSERT...ON DUPLICATE KEY UPDATE

                  INSERT INTO reports_adv (day, uid, siteid, cid, visits) 
                  VALUES ('$day', '$uid', '$sid', '$cid', 1)
                  ON DUPLICATE KEY UPDATE visits=visits+1;
                  

                  • INSERT ... ON DUPLICATE KEY UPDATE语法

                    但在此之前,您应该在列上定义 UNIQUE 约束.

                    but before anything else, you should define a UNIQUE constraint on the columns.

                    ALTER TABLE reports_adv  ADD CONSTRAINT tb_uq UNIQUE (day, uid, siteid, cid)
                    

                    这篇关于如果行存在,则更新 MySQL 表的最快方法,否则插入.超过 2 个非唯一键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何判断 MySQL UPDATE 何时成功与实际更新的数据? 下一篇:当用户未在文本框中输入任何内容时,表格会更新空格

                  相关文章

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

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

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