1. <small id='eSYd6'></small><noframes id='eSYd6'>

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

        MySQL 在插入/更新事件上触发

        MySQL trigger On Insert/Update events(MySQL 在插入/更新事件上触发)

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

              • <bdo id='N3K7X'></bdo><ul id='N3K7X'></ul>

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

                • 本文介绍了MySQL 在插入/更新事件上触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  所以我有两张这样的桌子...

                  So I have two tables like this...

                  ext_words
                  -------------
                  | id | word |
                  -------------
                  | 1  | this |
                  -------------
                  | 2  | that |
                  -------------
                  | 3  | this |
                  -------------
                  
                  ext_words_count
                  ---------------------
                  | id | word | count |
                  ---------------------
                  | 1  | this |   2   |
                  ---------------------
                  | 2  | that |   1   |
                  ---------------------
                  

                  我正在尝试创建一个触发器,它将:

                  I am trying to create a trigger that will:

                  • 在更新 ext_words.word 时更新 ext_words_count.count.
                  • update ext_words_count.count when ext_words.word is updated.

                  为了进一步复杂化,

                  • 如果ext_words.wordext_words更新时ext_words_count中不存在,我想把它插入到ext_words_count 并将 count 设置为 1.
                  • if ext_words.word does not exist in ext_words_count when ext_words is updated, I would like to insert it into ext_words_count and set count as 1.

                  我一直在看类似的问题:
                  1. 使用自增字段插入前/后触发器 和
                  2. 使用触发器更新另一个数据库中的表
                  试图结合 2.这是我目前所拥有的:

                  I have been looking at similar questions:
                  1. Before / after insert trigger using auto increment field, and
                  2. Using Trigger to update table in another database
                  trying to combine the 2. Here is what I have so far:

                  DELIMITER $$
                  CREATE TRIGGER update_count
                  AFTER UPDATE ON ext_words
                  FOR EACH ROW
                  BEGIN
                  
                    UPDATE ext_words_count
                      SET word_count = word_count + 1
                    WHERE word = NEW.word;
                  
                  END;
                  $$
                  DELIMITER ;
                  

                  非常感谢任何建议和指导.或者可能是我忽略的另一种方法,并一如既往地提前致谢!

                  Any advice and direction is greatly appreciated. Or possibly another method that I have overlooked and as always thanks in advance!

                  更新:
                  我选择使用 2 个触发器,一个用于 INSERT,一个用于 UPDATE,因为我不太熟悉 MySQL 中的条件语句.

                  UPDATE:
                  I have opted for using 2 triggers, one for INSERT and one for UPDATE because I am not that familiar with conditional statements in MySQL.

                  DELIMITER $$
                  CREATE TRIGGER insert_word AFTER INSERT ON ext_words
                    FOR EACH ROW
                      BEGIN
                        INSERT IGNORE INTO ext_words_count (word) VALUES (NEW.word);
                      END;
                  $$
                  DELIMITER ;
                  

                  DELIMITER $$
                  CREATE TRIGGER update_word AFTER UPDATE ON ext_words
                    FOR EACH ROW
                      BEGIN
                        UPDATE ext_words_count 
                        SET word_count = word_count + 1 
                        WHERE word = NEW.word;
                      END;
                  $$
                  DELIMITER ;
                  

                  INSERT 查询运行良好,但是 UPDATE 查询没有更新 word_count.我在更新查询中遗漏了什么吗..?

                  The INSERT query is working great, however the UPDATE query is not updating word_count. Is there something I missed in the update query..?

                  推荐答案

                  在 Grijesh 的完美帮助和他使用条件语句的建议下,我能够获得同时执行这两项任务的 ONE 触发器.再次感谢 Grijesh

                  With Grijesh's perfect help and his suggestion to use conditional statements, I was able to get ONE trigger that does both tasks. Thanks again Grijesh

                   DELIMITER $$ 
                   CREATE TRIGGER update_count AFTER INSERT ON ext_words 
                   FOR EACH ROW 
                     BEGIN
                       IF NOT EXISTS (SELECT 1 FROM ext_words_count WHERE word = NEW.word) THEN
                         INSERT INTO ext_words_count (word) VALUES (NEW.word);
                     ELSE
                         UPDATE ext_words_count SET word_count = word_count + 1 WHERE word = NEW.word;
                     END IF;
                    END $$    
                   DELIMITER;   
                  

                  这篇关于MySQL 在插入/更新事件上触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Can#39;t Create Entity Data Model - using MySql and EF6(无法创建实体数据模型 - 使用 MySql 和 EF6)
                  MySQL select with CONCAT condition(MySQL选择与CONCAT条件)
                  Capitalize first letter of each word, in existing table(将现有表格中每个单词的首字母大写)
                  How to retrieve SQL result column value using column name in Python?(如何在 Python 中使用列名检索 SQL 结果列值?)
                  Update row with data from another row in the same table(使用同一表中另一行的数据更新行)
                  Exporting results of a Mysql query to excel?(将 Mysql 查询的结果导出到 excel?)

                    <tbody id='sYQtG'></tbody>

                    1. <legend id='sYQtG'><style id='sYQtG'><dir id='sYQtG'><q id='sYQtG'></q></dir></style></legend>
                        <bdo id='sYQtG'></bdo><ul id='sYQtG'></ul>

                          • <small id='sYQtG'></small><noframes id='sYQtG'>

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