简体   繁体   English

使用“NEW.idtable”插入一个表的所有 id 以及另一个表的最后插入的 id

[英]INSERT all the ids of a table together with the last inserted id of another using “NEW.idtable”

I have 3 tables: The first is called "paciente" it has an ID as the primary key which is called idpaciente The second is called "sessao", in it I want to add the current date every day and also has an id called idsessao And I have the third one called sessao_has_paciente which is the N: M relationship table between these two tables.我有 3 个表:第一个称为“paciente”,它有一个 ID 作为主键,称为 idpaciente 第二个称为“sessao”,我想在其中每天添加当前日期,并且还有一个名为 idsessao 的 ID我还有第三个叫做 sessao_has_paciente,它是这两个表之间的 N:M 关系表。 The question I have is this: I insert the current date in the sessao table according to an event that runs every 1 day, and I put a trigger that after inserting in that table would list the idsessao of the second table with all the idpaciente of the first in the third table called `sessao_has_paciente.我的问题是:我根据每 1 天运行一次的事件在 sessao 表中插入当前日期,并放置一个触发器,在插入该表后将列出第二个表的 idsessao 以及所有 idpaciente第三个表中的第一个称为`sessao_has_paciente。

My trigger code looks like this:我的触发代码如下所示:

INSERT INTO sessao_has_paciente SELECT idpaciente from paciente, NEW.idsessao 

The error that MySQL returns is that there is no NEW.idsessao field, without this NEW.idsessao field the INSERT works normally but I need to have this to insert the last session table ID for all rows of the paciente table. MySQL 返回的错误是没有 NEW.idsessao 字段,没有这个 NEW.idsessao 字段,INSERT 工作正常,但我需要有这个才能为 paciente 表的所有行插入最后一个 session 表 ID。

CREATE TABLE 1创建表 1

CREATE TABLE paciente (
  idpaciente int(11) NOT NULL AUTO_INCREMENT,
  dia_iddia int(11) DEFAULT NULL,
  turno_idturno int(11) DEFAULT NULL,
  medico_idmedico int(11) DEFAULT NULL,
  tiposanguineo_idtiposanguineo int(11) DEFAULT NULL,
  duracaohd_idduracaohd int(11) DEFAULT NULL,
  frequenciahd_idfrequenciahd int(11) DEFAULT NULL,
  cidade_idcidade int(11) DEFAULT NULL,
  estado_idestado int(11) DEFAULT NULL,
  profissao_idprofissao int(11) DEFAULT NULL,
  tiposlogradouro_idtiposlogradouro int(11) DEFAULT NULL,
  nome varchar(45) DEFAULT NULL,
  apelido varchar(45) DEFAULT NULL,
  bairro varchar(45) DEFAULT NULL,
  cep varchar(45) DEFAULT NULL,
  cpf varchar(11) DEFAULT NULL,
  datanascimento date DEFAULT NULL,
  datainiciohd date DEFAULT NULL,
  endereco varchar(100) DEFAULT NULL,
  numero varchar(10) DEFAULT NULL,
  foto varchar(45) DEFAULT NULL,
  pesoseco float DEFAULT NULL,
  PRIMARY KEY (idpaciente),
  KEY fk_paciente_tiposanguineo1_idx (tiposanguineo_idtiposanguineo),
  KEY fk_paciente_duracaohd1_idx (duracaohd_idduracaohd),
  KEY fk_paciente_frequenciahd1_idx (frequenciahd_idfrequenciahd),
  KEY fk_paciente_cidade1_idx (cidade_idcidade),
  KEY fk_paciente_profissao1_idx (profissao_idprofissao),
  KEY fk_paciente_tiposlogradouro1_idx (tiposlogradouro_idtiposlogradouro),
  KEY fk_paciente_turno1_idx (turno_idturno),
  KEY fk_paciente_medico1_idx (medico_idmedico),
  KEY fk_paciente_dia1_idx (dia_iddia),
  KEY fk_paciente_estado1_idx (estado_idestado),
  CONSTRAINT fk_paciente_cidade1 FOREIGN KEY (cidade_idcidade) REFERENCES cidade (idcidade) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT fk_paciente_dia1 FOREIGN KEY (dia_iddia) REFERENCES dia (iddia) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT fk_paciente_duracaohd1 FOREIGN KEY (duracaohd_idduracaohd) REFERENCES duracaohd (idduracaohd) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT fk_paciente_estado1 FOREIGN KEY (estado_idestado) REFERENCES estado (idestado) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT fk_paciente_frequenciahd1 FOREIGN KEY (frequenciahd_idfrequenciahd) REFERENCES frequenciahd (idfrequenciahd) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT fk_paciente_medico1 FOREIGN KEY (medico_idmedico) REFERENCES medico (idmedico) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT fk_paciente_profissao1 FOREIGN KEY (profissao_idprofissao) REFERENCES profissao (idprofissao) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT fk_paciente_tiposanguineo1 FOREIGN KEY (tiposanguineo_idtiposanguineo) REFERENCES tiposanguineo (idtiposanguineo) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT fk_paciente_tiposlogradouro1 FOREIGN KEY (tiposlogradouro_idtiposlogradouro) REFERENCES tiposlogradouro (idtiposlogradouro) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT fk_paciente_turno1 FOREIGN KEY (turno_idturno) REFERENCES turno(idturno) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=39 DEFAULT CHARSET=utf8;

TRIGGER TABLE 1:触发器表 1:

  CREATE DEFINER=ConexaoClinica@% TRIGGER testemodeldb2.paciente_AFTER_INSERT AFTER INSERT ON paciente FOR EACH ROW
    BEGIN
    INSERT INTO fichasala (idfichasala, paciente_idpaciente) VALUES (DEFAULT, NEW.idpaciente);

END

This Trigger is working normally as it should.此触发器正常工作。

TABLE 2 CREATE表 2 创建

 CREATE TABLE sessao (
      idsessao int(11) NOT NULL AUTO_INCREMENT,
      diasessao date DEFAULT NULL,
      PRIMARY KEY (idsessao)
    ) ENGINE=InnoDB AUTO_INCREMENT=36 DEFAULT CHARSET=utf8;

TRIGGER TABLE 2:触发器表 2:

CREATE DEFINER=ConexaoClinica@% TRIGGER testemodeldb2.sessao_AFTER_INSERT AFTER INSERT ON sessao FOR EACH ROW
BEGIN
INSERT INTO sessao_has_paciente (paciente_idpaciente, sessao_idsessao) SELECT idpaciente from paciente, new.idsessao;
END

TABLE 3 CREATE:表 3 创建:

CREATE TABLE sessao_has_paciente (
  idsessao_has_paciente int(11) NOT NULL AUTO_INCREMENT,
  sessao_idsessao int(11) DEFAULT NULL,
  paciente_idpaciente int(11) DEFAULT NULL,
  PRIMARY KEY (idsessao_has_paciente),
  KEY fk_sessao_has_paciente_paciente1_idx (paciente_idpaciente),
  KEY fk_sessao_has_paciente_sessao1_idx (sessao_idsessao),
  CONSTRAINT fk_sessao_has_paciente_paciente1 FOREIGN KEY (paciente_idpaciente) REFERENCES paciente (idpaciente) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT fk_sessao_has_paciente_sessao1 FOREIGN KEY (sessao_idsessao) REFERENCES sessao (idsessao) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;

Here is my problem, this "NEW.idsessao" Only works correctly when I perform this trigger on table 2 SESSAO :这是我的问题,这个"NEW.idsessao"只有在我对表 2 SESSAO执行这个触发器时才能正常工作:

CREATE DEFINER=ConexaoClinica@% TRIGGER testemodeldb2.sessao_AFTER_INSERT` AFTER INSERT ON sessao FOR EACH ROW
BEGIN
INSERT INTO sessao_has_paciente (paciente_idpaciente, sessao_idsessao) VALUES ('1', new.idsessao);
END

In other words, I have to enter the ID manually, I didn't want this I wanted all IDS in table 1 to be inserted with only the last ID inserted in table 2 in the third table, and I found the way to do this through a subquery and here's the trigger I tried:换句话说,我必须手动输入 ID,我不想这样我希望插入表 1 中的所有 IDS,只插入第三个表中表 2 中的最后一个 ID,我找到了这样做的方法通过子查询,这是我尝试的触发器:

CREATE DEFINER=ConexaoClinica@% TRIGGER testemodeldb2.sessao_AFTER_INSERT AFTER INSERT ON sessao FOR EACH ROW
BEGIN
INSERT INTO sessao_has_paciente (paciente_idpaciente, sessao_idsessao) SELECT idpaciente from paciente, new.idsessao;
END

But mysql returns the error saying that the field new.idsessao does not exist...但是 mysql 返回错误,指出字段 new.idsessao 不存在...

If I do this same trigger mentioned above without NEW.idsessao it works normally, that is, each one of them works in a different way but I wanted them to work together to be able to make this insert.如果我在没有 NEW.idsessao 的情况下执行上述相同的触发器,则它可以正常工作,也就是说,它们中的每一个都以不同的方式工作,但我希望它们一起工作以能够进行此插入。

MODEL OF DB MODEL 数据库

I'll explain everything I want to do from the beginning so that you can understand well.我将从一开始就解释我想做的所有事情,以便您能很好地理解。

I want to do a session automation.我想做一个 session 自动化。

How I thought about doing this: Create an event that every 1 day if the day is between MONDAY TO SATURDAY, an insert will be made with the current date in the diasessao field.我是如何考虑这样做的:如果这一天在星期一到星期六之间,则每 1 天创建一个事件,将在diasessao字段中插入当前日期。

delimiter |

CREATE EVENT added
ON SCHEDULE EVERY 1 DAY
STARTS '2021-02-21 00:10:00'
OF
  IF DAYOFWEEK (curdate ()) IN (2, 3, 4, 5, 6, 7) THEN
   INSERT INTO session VALUES (DEFAULT, CURDATE ());
  END IF;
  delimiter;

When something is inserted in the SESSION table it will trigger a trigger that takes ALL the IDS from the PATIENT table and correlates with the LAST ID INSERTED in that SESSION table and all this goes to table 3 PATIENTE_HAS_SESSAO , that is, for each id in the PATIENT table a line will be made with the patient ID + the last ID inserted in the SESSION table当在SESSION表中插入某些内容时,它将触发一个触发器,该触发器从PATIENT表中获取所有 IDS,并与SESSION表中的 LAST ID INSERTED 相关联,所有这些都转到表 3 PATIENTE_HAS_SESSAO中,即对于每个PATIENT表将使用患者 ID + 插入SESSION表中的最后一个 ID 创建一行

But patients are also separated by DAYS OF THE WEEK, there are patients who have sessions on MONDAY, WEDNESDAY and FRIDAY and others TUESDAY, THURSDAY AND SATURDAY, this is fixed, it does not vary so I would have to do a WHERE with the IF in this trigger just as I did with the event the whole problem is that I am not able to get the LAST INSERT as I did on the trigger of PATIENT = INSERT INTO fichasala (idfichasala, patient_idpaciente) VALUES (DEFAULT, NEW.idpaciente);但是患者也按星期几分开,有些患者在星期一,星期三和星期五以及其他星期二,星期四和星期六进行会议,这是固定的,它没有变化,所以我必须用 IF 做一个 WHERE在这个触发器中,就像我对事件所做的那样,整个问题是我无法像在PATIENT = INSERT INTO fichasala (idfichasala, patient_idpaciente) VALUES (DEFAULT, NEW.idpaciente); In this case, when I use a SUBQUERY, MySQL says that the NEW.idsessao field does not exist.在这种情况下,当我使用 SUBQUERY 时,MySQL 说 NEW.idsessao 字段不存在。

I've never heard of this NEW.tablename in mysql.我从未听说过 mysql 中的这个 NEW.tablename。 What you can do is retrieve the last inserted ID from a specific table like this:您可以做的是从特定表中检索最后插入的 ID,如下所示:

SELECT id FROM sessao ORDER BY date DESC LIMIT 1

OK that is much better.好的,那好多了。

The Trigger must be different, because your solution produced a syntax error触发器必须不同,因为您的解决方案产生了语法错误

CREATE DEFINER=ConexaoClinica@% TRIGGER testemodeldb2.sessao_AFTER_INSERT AFTER INSERT ON sessao FOR EACH ROW
BEGIN
INSERT INTO sessao_has_paciente (paciente_idpaciente, sessao_idsessao) SELECT idpaciente, new.idsessao from paciente;
END

delimiter |分隔符 |

For your event only enter the curdate without the autoincrment field对于您的活动,仅输入不带自动增量字段的 curdate

CREATE EVENT added
ON SCHEDULE EVERY 1 DAY
STARTS '2021-02-21 00:10:00'
DO
  IF DAYOFWEEK (curdate ()) IN (2, 3, 4, 5, 6, 7) THEN
   INSERT INTO session (`diasessao`) VALUES ( CURDATE());
  END IF;
  delimiter;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在另一个表中插入最后插入的ID - How to Insert last inserted id in another table 使用Codeigniter将最后插入的记录ID插入另一个表的数组中 - insert the last inserted record id into an array in another table using Codeigniter 检索表的最后插入ID以在另一个表中用作外键 - retrieving last inserted ID of a table for using as Foreign key in another table mysql,java - 从一个表中获取最后插入的自动增量 id 并将其再次插入到另一个表中 - mysql, java - Get last inserted auto increment id from one table and insert it again on another table 如何获取一个表上的最后一个插入ID,以在另一个表上的另一个插入中使用它? - How to get the last inserted id on a table to use it on another insert on another table? MySQL查询-获取未插入的ID以向表中插入新行 - MySQL query - getting not inserted ids to insert new row to a table java,mysql-为什么不工作,从一个表中获取最后一个插入的ID,然后将其再次插入另一个表中? - java,mysql - why not working get last inserted id from one table and insert it again on another table? 想要将一个表的最后插入的ID插入laravel 5中的另一个表 - Want to insert one table's last inserted id into another table in laravel 5 使用MYSQL LAST_INSERT_ID()来检索插入行的ID? - Using MYSQL LAST_INSERT_ID() to retrive the ID of inserted row? SQL查询以在新表中为另一个表的所有ID插入相同记录 - SQL query to insert the same record for all the ids of another table in a new table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM