- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我搜索了很多次,寻找任何技巧或解决方法来在 MySQL 中像 Oracle 那样制作“语句级”触发器,但我没有找到。所以我想出了这个技巧,它对我有用,希望它可以帮助任何人
请参阅下面我的回答。
最佳答案
假设我们想要插入多行,并且想要一次性执行某件事,但我们无法避免 MySQL 触发器中的“FOR EACH ROW”
`insert to table1 (sname,age) VALUES ('mariam',5),('jojo',3),('ahmed',29)`
statementidstable
(ID
,statementid
)其次,您必须使用软件为您的账单准备一个唯一 ID
假设是“123456”。
将您的声明更改为
INSERT INTO table1 (sname,age,uniqueid) VALUES ('mariam',5,123456),('jojo',3,123456),('ahmed',29,123456)
CREATE TRIGGER
table1_after_insert
AFTER INSERT
ON
table1
FOR EACH ROW
BEGIN
DECLARE isfired tinyint(1);
SELECT COUNT(statementid) INTO isfired from statementidstable where statementid=NEW.uniqeid LIMIT 1;
IF isfired = 0 THEN
'''DO WHAT YOU WANT HERE because this is the first time for this statement id
'''then insert the statementid to statementidstable
INSERT INTO statementidstable (statementid) VALUES (NEW.uniqeid)
ELSE
'''Nothing will happen becaue you already have the statement id in statementidstable
END IF;
END;
完成后从statementidstable中删除statementid(用您的软件处理)
** 另一个技巧,你可以做到这一点,不需要statementid,而且你的触发器可以在仅插入第一行后触发一次,或者在仅插入最后一行或两者都插入后触发一次
在这个技巧中,你必须像这样准备你的陈述
`insert to table1 (sname,age,rowid) VALUES ('mariam',5,1),('jojo',3,0),('dodo',3,0),('ahmed',29,-1)`
第一行有 rowid=1 (与任何其他行不同)使您的触发器知道它将在插入第一行后触发一次。
最后一行有 rowid=-1 (与任何其他行不同)让您的触发器知道它会在插入最后一行后触发一次。
其他行的 rowid=0 或 (1,-1) 之外的任何其他值
CREATE TRIGGER
table1_after_insert
AFTER INSERT
ON
table1
FOR EACH ROW
BEGIN
IF rowid = 1 THEN
'''DO WHAT YOU WANT HERE after inserting first row only
ELSEIF rowid = -1
'''DO WHAT YOU WANT HERE after inserting last row only
ELSE
'''NOTHING WILL HAPPEN
END IF;
END;
关于Mysql 'statement level' 触发器...你可以用两个技巧来做到这一点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44297014/
在 go lang 中使用“If with a short statement”有什么好处。引用:go tour if v := math.Pow(x, n); v < lim { retur
完全错误: Warning: Unsafe statement written to the binary log using statement format since BINLOG_FORMAT
完全错误: Warning: Unsafe statement written to the binary log using statement format since BINLOG_FORMAT
我有三个存储过程 Sp1、Sp2 和 Sp3。 第一个 (Sp1) 将执行第二个 (Sp2) 并将返回的数据保存到 @tempTB1 中,第二个将执行第三个 (Sp3) 并将数据保存到 @tempTB
我已将 FLAG 设置为 1,并且正在执行 ARG 值应该仅为 DEV。但是我得到的是 ARG= DEV + CLIENTID 000023 // FLAG=1 000026 // I
我已将 FLAG 设置为 1,并且正在执行 ARG 值应该仅为 DEV。但是我得到的是 ARG= DEV + CLIENTID 000023 // FLAG=1 000026 // I
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
PMD告诉我 A switch with less than 3 branches is inefficient, use a if statement instead. 这是为什么呢?为什么是3?他
我刚开始学习 Racket,所以我仍在努力弄清楚这门语言的复杂性。我正在尝试在列表中实现我自己的搜索功能。如果函数找到它,则返回索引,否则返回 -1。 (define (find-index list
在 Kotlin 中,您可以使用类似于三元运算符的 if 语句。 我们可以选择做这样的事情: val x = if (isOdd) 1 else 2 但是如果我们有多个变量需要根据某些条件进行设置,那
在我的 Android 应用程序中,我尝试使用 XMLPullParser 使用以下代码读取 xml 文件: while (eventType != XmlPullParser.END_DOCUMEN
这个问题已经有答案了: Boolean expressions in Java (8 个回答) 已关闭 7 年前。 我遇到过一些情况,其中第一个似乎改变了 boolean 值,而第二个却没有!两者之间
我基本上想做的是: select * from request where id = 1 and created_at like (today's date); 但使用 Eloquent 。 我试过:
我不确定为什么会收到此代码。基本上我希望能够动态定位我的发射器,但是当我添加一个选项来检查位置并根据需要进行纠正时,我不断收到此错误。 添加的代码是 if (
何时使用语句而不是准备语句。我想语句用于没有参数的查询,但为什么不使用准备好的语句呢?对于没有参数的查询,哪个更快。 最佳答案 I suppose statement is used in queri
我必须创建一个表,如下所示 借款人(客户编号,贷款编号) 如果客户没有超过 3 笔贷款,则可以贷款。 我创建的表如下 create table borrower( customerno int(5),
这个问题在这里已经有了答案: 关闭 12 年前。 Possible Duplicates: Is "else if" faster than "switch() case"? What is the
typescript 版本 2.2.2 我在我的 UserRoutzr.ts 中写了这个要求 const users = require(path.join(process.cwd() + "/da
我有一个用 JPQL 编写的应用程序,它可以命中非常不同的查询(在不同的资源上)。 对于很多此类查询,我需要知道结果总数(计数),因为我没有应用任何 LIMIT/OFFSET 由于此查询的性质非常不同
我对以下 Java 语句感到困惑: ArtClass artClass0 = new ArtClass(); int int3 = 73; boolean boolean0 = artClass0.f
我是一名优秀的程序员,十分优秀!