phpmyadmin 触发器中的“NEW”表有时包含列,有时不包含

du7egjpx  于 2022-11-09  发布在  PHP
关注(0)|答案(2)|浏览(173)

我一直在尝试创建一个INSERT触发器,在一个简单的触发器体中,我可以很好地访问“NEW”表。
但是在一个例子中,“NEW”表被用在正文的更深处,它不再找到我需要的列。
我可以在以下位置找到“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表中插入了一个新行,其中100%包含“nr_legitimatie”列。
玛丽亚数据库10.4

yyhrrdl8

yyhrrdl81#

我在我的手机上,所以还没有测试过这个,但也许你不能在同一个触发器中引用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
wfsdck30

wfsdck302#

这是一个MariaDB bug(请特别参阅此注解,其中有一个与您的问题相关的可重现示例),带有一个误导性的错误消息。
当您重复使用使用new的CTE时,触发程序中会发生Bug。
在您的例子中,您在medii_union中使用medii_pe_coloane(它使用new)3次,在date的最后选择中使用一次,这就触发了bug。
但是,您可以根据需要经常使用new本身(在任意数量的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

使您的代码有点重复,但应该作为一个通用的解决方案。或者,您可以重写和/或简化您的查询,否则(请参阅例如MatBailies answer)。

相关问题