触发器中的 "NEW" table 有时包含列,有时不包含
The "NEW" table in a trigger sometimes contains column and sometimes it does not
我一直在尝试创建一个 INSERT 触发器,在一个简单的触发器主体中,我可以访问“NEW”table 就好了。
但是在一个示例中,“新”table 用于更深的主体,它不再找到我需要的列。
我可以在以下位置找到列“NEW.nr_legitimatie”:
CREATE DEFINER=`root`@`localhost`
TRIGGER `calculMedie`
AFTER INSERT ON `note`
FOR EACH ROW
INSERT INTO medii (MEDII.nr_legitimatie, MEDII.medie_generala, medii.medie_an1, medii.medie_an2, medii.medie_an3)
VALUES (new.nr_legitimatie, 1, 2, 3, 4)
我在以下位置找不到列“NEW.nr_legitimatie”:
CREATE DEFINER=`root`@`localhost`
TRIGGER `calculMedie`
AFTER INSERT ON `note`
FOR EACH ROW
INSERT INTO medii (MEDII.nr_legitimatie, MEDII.medie_generala, medii.medie_an1, medii.medie_an2, medii.medie_an3)
WITH date AS (
WITH medii_pe_coloane AS (
SELECT medie1.nr_legitimatie,
AVG(medie1.maxim) as medie_an_1,
AVG(medie2.maxim) as medie_an_2,
AVG(medie3.maxim) as medie_an_3
FROM
(
SELECT nr_legitimatie, disciplina, an_studiu, MAX(nota) AS maxim
FROM note
WHERE an_studiu = 1 AND nr_legitimatie = new.nr_legitimatie
GROUP BY nr_legitimatie, disciplina
) as medie1,
(
SELECT nr_legitimatie, disciplina, an_studiu, MAX(nota) AS maxim
FROM note
WHERE an_studiu = 2 AND nr_legitimatie = new.nr_legitimatie
GROUP BY nr_legitimatie, disciplina
) as medie2,
(
SELECT nr_legitimatie, disciplina, an_studiu, MAX(nota) AS maxim
FROM note
WHERE an_studiu = 3 AND nr_legitimatie = new.nr_legitimatie
GROUP BY nr_legitimatie, disciplina
) as medie3
),
medii_union AS (
SELECT medii_pe_coloane.medie_an_1 as medie from medii_pe_coloane
UNION ALL
SELECT medii_pe_coloane.medie_an_2 as medie from medii_pe_coloane
UNION ALL
SELECT medii_pe_coloane.medie_an_3 as medie from medii_pe_coloane
),
medie_generala AS (
SELECT AVG(medii_union.medie) as medie FROM medii_union
)
SELECT medii_pe_coloane.*, medie_generala.medie from medii_pe_coloane, medie_generala
)
SELECT nr_legitimatie, medie, medie_an_1, medie_an_2, medie_an_3
FROM date
我无法理解可能发生的事情。它没有被覆盖。也许它超出了它的范围?我怎么可能在 WHERE 子句中使用它?
错误是:
#1054 - Unknown column 'nr_legitimatie' in 'NEW'
为了测试它,我在 note
table 中插入了一个新行,其中 100% 包含“nr_legitimatie”列。
MariaDB 10.4
我在 phone 所以还没有测试过这个,但也许你不能在同一个触发器中引用 NEW 三次?
如果是这样,我会重写您的查询,无论如何它看起来都有逻辑错误。
这是我建议的查询...
INSERT INTO
medii (
MEDII.nr_legitimatie, MEDII.medie_generala, medii.medie_an1, medii.medie_an2, medii.medie_an3
)
WITH
max_an_studiu AS
(
SELECT nr_legitimatie, disciplina, an_studiu, MAX(nota) AS maxim
FROM note
WHERE an_studiu IN (1, 2, 3)
AND nr_legitimatie = (SELECT nr_legitimatie FROM new)
GROUP BY nr_legitimatie, disciplina, an_studiu
),
medie AS
(
SELECT
nr_legitimatie,
an_studiu,
AVG(maxim) AS medie
FROM
max_an_studiu
GROUP BY
nr_legitimatie,
an_studiu
)
SELECT
nr_legitimatie,
AVG(medie) AS medie,
MAX(CASE WHEN an_studiu=1 THEN medie END) AS medie_an_1,
MAX(CASE WHEN an_studiu=2 THEN medie END) AS medie_an_2,
MAX(CASE WHEN an_studiu=3 THEN medie END) AS medie_an_3
FROM
medie
GROUP BY
nr_legitimatie
这是一个 MariaDB bug (see especially this comment 具有接近您的问题的可重现示例)带有误导性错误消息。
当您重用使用 new
的 CTE 时,您的错误发生在触发器中。
在您的情况下,您在 medii_union
中使用了 medii_pe_coloane
(即使用 new
)3 次,在 date
的最后 select 中使用了一次.这会触发错误。
不过,您可以随意使用 new
本身(在任意多的 CTE 中)。所以解决方法是多次重新定义 CTE,
例如像
WITH medii_pe_coloane AS ( <query using "new"> ),
medii_pe_coloane1 AS ( <same query using "new"> ),
medii_pe_coloane2 AS ( <same query using "new"> ),
medii_pe_coloane3 AS ( <same query using "new"> ),
...
medii_union AS (
select medii_pe_coloane1.medie_an_1 ... from medii_pe_coloane1
union all select medii_pe_coloane2.medie_an_2 ... from medii_pe_coloane2
union all select medii_pe_coloane3.medie_an_3 ... from medii_pe_coloane3)
...
SELECT ... from medii_pe_coloane, medie_generala
使您的代码有点重复,但应该作为一般解决方法使用。或者,您可以重写 and/or 以其他方式简化您的查询(参见 )。
我一直在尝试创建一个 INSERT 触发器,在一个简单的触发器主体中,我可以访问“NEW”table 就好了。
但是在一个示例中,“新”table 用于更深的主体,它不再找到我需要的列。
我可以在以下位置找到列“NEW.nr_legitimatie”:
CREATE DEFINER=`root`@`localhost`
TRIGGER `calculMedie`
AFTER INSERT ON `note`
FOR EACH ROW
INSERT INTO medii (MEDII.nr_legitimatie, MEDII.medie_generala, medii.medie_an1, medii.medie_an2, medii.medie_an3)
VALUES (new.nr_legitimatie, 1, 2, 3, 4)
我在以下位置找不到列“NEW.nr_legitimatie”:
CREATE DEFINER=`root`@`localhost`
TRIGGER `calculMedie`
AFTER INSERT ON `note`
FOR EACH ROW
INSERT INTO medii (MEDII.nr_legitimatie, MEDII.medie_generala, medii.medie_an1, medii.medie_an2, medii.medie_an3)
WITH date AS (
WITH medii_pe_coloane AS (
SELECT medie1.nr_legitimatie,
AVG(medie1.maxim) as medie_an_1,
AVG(medie2.maxim) as medie_an_2,
AVG(medie3.maxim) as medie_an_3
FROM
(
SELECT nr_legitimatie, disciplina, an_studiu, MAX(nota) AS maxim
FROM note
WHERE an_studiu = 1 AND nr_legitimatie = new.nr_legitimatie
GROUP BY nr_legitimatie, disciplina
) as medie1,
(
SELECT nr_legitimatie, disciplina, an_studiu, MAX(nota) AS maxim
FROM note
WHERE an_studiu = 2 AND nr_legitimatie = new.nr_legitimatie
GROUP BY nr_legitimatie, disciplina
) as medie2,
(
SELECT nr_legitimatie, disciplina, an_studiu, MAX(nota) AS maxim
FROM note
WHERE an_studiu = 3 AND nr_legitimatie = new.nr_legitimatie
GROUP BY nr_legitimatie, disciplina
) as medie3
),
medii_union AS (
SELECT medii_pe_coloane.medie_an_1 as medie from medii_pe_coloane
UNION ALL
SELECT medii_pe_coloane.medie_an_2 as medie from medii_pe_coloane
UNION ALL
SELECT medii_pe_coloane.medie_an_3 as medie from medii_pe_coloane
),
medie_generala AS (
SELECT AVG(medii_union.medie) as medie FROM medii_union
)
SELECT medii_pe_coloane.*, medie_generala.medie from medii_pe_coloane, medie_generala
)
SELECT nr_legitimatie, medie, medie_an_1, medie_an_2, medie_an_3
FROM date
我无法理解可能发生的事情。它没有被覆盖。也许它超出了它的范围?我怎么可能在 WHERE 子句中使用它?
错误是:
#1054 - Unknown column 'nr_legitimatie' in 'NEW'
为了测试它,我在 note
table 中插入了一个新行,其中 100% 包含“nr_legitimatie”列。
MariaDB 10.4
我在 phone 所以还没有测试过这个,但也许你不能在同一个触发器中引用 NEW 三次?
如果是这样,我会重写您的查询,无论如何它看起来都有逻辑错误。
这是我建议的查询...
INSERT INTO
medii (
MEDII.nr_legitimatie, MEDII.medie_generala, medii.medie_an1, medii.medie_an2, medii.medie_an3
)
WITH
max_an_studiu AS
(
SELECT nr_legitimatie, disciplina, an_studiu, MAX(nota) AS maxim
FROM note
WHERE an_studiu IN (1, 2, 3)
AND nr_legitimatie = (SELECT nr_legitimatie FROM new)
GROUP BY nr_legitimatie, disciplina, an_studiu
),
medie AS
(
SELECT
nr_legitimatie,
an_studiu,
AVG(maxim) AS medie
FROM
max_an_studiu
GROUP BY
nr_legitimatie,
an_studiu
)
SELECT
nr_legitimatie,
AVG(medie) AS medie,
MAX(CASE WHEN an_studiu=1 THEN medie END) AS medie_an_1,
MAX(CASE WHEN an_studiu=2 THEN medie END) AS medie_an_2,
MAX(CASE WHEN an_studiu=3 THEN medie END) AS medie_an_3
FROM
medie
GROUP BY
nr_legitimatie
这是一个 MariaDB bug (see especially this comment 具有接近您的问题的可重现示例)带有误导性错误消息。
当您重用使用 new
的 CTE 时,您的错误发生在触发器中。
在您的情况下,您在 medii_union
中使用了 medii_pe_coloane
(即使用 new
)3 次,在 date
的最后 select 中使用了一次.这会触发错误。
不过,您可以随意使用 new
本身(在任意多的 CTE 中)。所以解决方法是多次重新定义 CTE,
例如像
WITH medii_pe_coloane AS ( <query using "new"> ),
medii_pe_coloane1 AS ( <same query using "new"> ),
medii_pe_coloane2 AS ( <same query using "new"> ),
medii_pe_coloane3 AS ( <same query using "new"> ),
...
medii_union AS (
select medii_pe_coloane1.medie_an_1 ... from medii_pe_coloane1
union all select medii_pe_coloane2.medie_an_2 ... from medii_pe_coloane2
union all select medii_pe_coloane3.medie_an_3 ... from medii_pe_coloane3)
...
SELECT ... from medii_pe_coloane, medie_generala
使您的代码有点重复,但应该作为一般解决方法使用。或者,您可以重写 and/or 以其他方式简化您的查询(参见