mysql中什么时候用for each row 什么时候用for each statement
时间: 2023-03-03 16:38:34 浏览: 152
在MySQL中,使用"for each row"和"for each statement"可以分别控制触发器何时被触发。
当需要针对每行数据进行操作时,应该使用"for each row"。这种情况通常包括在一个表中插入、更新或删除行时需要执行的操作。
而当需要在执行单个语句时进行操作,而不是针对每一行数据时,应该使用"for each statement"。这种情况通常包括在整个表上执行某些操作,而不是针对特定行进行操作。
因此,在编写触发器时,应该根据所需的操作和触发时机来选择使用"for each row"或"for each statement"。
相关问题
不写 FOR EACH ROW情况
在 MySQL 中,如果您在创建触发器时未指定 `FOR EACH ROW`,则默认为 `FOR EACH STATEMENT`。这意味着 MySQL 只会为每个 SQL 语句执行一次触发器中的操作,而不是为每个受影响的行执行。
以下是一个示例,在 `orders` 表上创建一个 `BEFORE INSERT` 触发器,每次插入新行时都会更新 `order_total` 字段的值,但未指定 `FOR EACH ROW`:
```sql
CREATE TRIGGER update_order_total BEFORE INSERT ON orders
BEGIN
UPDATE orders SET order_total = price * quantity;
END;
```
在这个例子中,当您执行一个插入语句时,MySQL 只会更新一次 `orders` 表中的所有行的 `order_total` 字段,而不是为每个新插入的行计算和更新 `order_total`。
因此,如果您需要在触发器中为每个受影响的行执行操作,则必须使用 `FOR EACH ROW` 语法。
This is a MySQL query that selects the student ID (s_id), assigns a sequential number to each row (i), and calculates the rank of each student based on their sum of scores (sum_score). The query uses a subquery to first group the scores by student ID and calculate the sum of scores for each student. This subquery is then joined with a variable initialization subquery that sets the initial values of @k, @i, and @score to 0. The variable @k is used to keep track of the current rank while iterating over the rows. The variable @i is used to assign a sequential number to each row. The variable @score is used to compare the sum_score of the current row with the sum_score of the previous row. The CASE statement is used to check if the sum_score of the current row is equal to the sum_score of the previous row. If they are equal, then the rank remains the same. If they are not equal, then the rank is updated to the current sequential number. Here is a breakdown of the query: 复制 SELECT a.s_id, -- Select the student ID @i:=@i+1 AS i, -- Assign a sequential number to each row @k:=(case when @score=a.sum_score then @k else @i end) as rank, -- Calculate the rank a.sum_score AS score -- Select the sum of scores for each student FROM (SELECT s_id,SUM(s_score) AS sum_score FROM score GROUP BY s_id ORDER BY sum_score DESC) a, -- Subquery to calculate sum of scores for each student (SELECT @k:=0,@i:=0,@score:=0) s -- Subquery to initialize variables Note that the use of variables in this query is not recommended, as it can lead to unexpected results if the variables are not reset properly. It is better to use a subquery or a window function to calculate the rank. 翻译
这是一个MySQL查询,它选择学生ID(s_id),为每一行分配一个连续的数字(i),并根据学生得分总和(sum_score)计算每个学生的排名。查询使用一个子查询首先按学生ID分组并计算每个学生的得分总和。然后,将此子查询与变量初始化子查询连接,该子查询将@k,@i和@score的初始值设置为0。变量@k用于在迭代行时跟踪当前排名。变量@i用于为每行分配一个连续的数字。变量@score用于将当前行的sum_score与上一行的sum_score进行比较。CASE语句用于检查当前行的sum_score是否等于上一行的sum_score。如果它们相等,则排名保持不变。如果它们不相等,则将排名更新为当前的连续数字。以下是查询的详细说明:
```
SELECT
a.s_id, -- 选择学生ID
@i:=@i+1 AS i, -- 为每一行分配一个连续的数字
@k:=(case when @score=a.sum_score then @k else @i end) as rank, -- 计算排名
a.sum_score AS score -- 选择每个学生的得分总和
FROM
(SELECT s_id,SUM(s_score) AS sum_score FROM score GROUP BY s_id ORDER BY sum_score DESC) a, -- 子查询计算每个学生的得分总和
(SELECT @k:=0,@i:=0,@score:=0) s -- 变量初始化子查询
```
请注意,不建议在查询中使用变量,因为如果变量没有正确重置,可能会导致意外的结果。最好使用子查询或窗口函数来计算排名。
阅读全文