gpt4 book ai didi

javascript - 使用 NodeJS 在 mysql 中插入关系表的最佳方法是什么

转载 作者:行者123 更新时间:2023-11-29 10:03:41 25 4
gpt4 key购买 nike

首先我不得不说我在 NodeJS 技术方面完全是新手。但是,我尝试做一些事情来尝试学习它。

这就是问题所在:我有 3 个表(PARTICIPANT、ADDRESS_PARTICIPANT 和 INSCRIPTION)。

ADDRESS_PARTICIPANT 包含participant_id(它是参与者的地址)。

INSCRIPTION 包含participant_id

因此,要存储铭文行,首先我需要保存 PARTICIPANT 和 ADDRESS_PARTICIPANT。只有在此之后我才能插入 INSCRIPTION

我正在按照我所学的方式这样做,但我认为有很多嵌套的 if。

我该如何改进这段代码?有人告诉我,“我会很好”,但我不知道。有人可以帮助我吗?谢谢

代码如下:

this.save = function(data, retur) {
var con = db();
const SQL_INSERT_PARTICIPANT =
`INSERT INTO participant (nome_completo, tipo_funcionario, data_nascimento, sexo, unidade, cpf, email, telefone, telefone_emergencia) VALUES( ? )` ;
const SQL_INSERT_ADDRESS_PARTICIPANT =
`INSERT INTO endereco_participante (participant_id, cep, estado, cidade, bairro, endereco, numero) values( ? )`;

const SQL_INSERT_INSCRIPTIONS = `......`

var values = [
data.nome_completo, data.tipo_funcionario, new Date(dateToEN(data.data_nascimento)), data.sexo, data.unidade, data.cpf_funcionario, data.email, data.telefone, data.telefone_emergencia
]

const insertParticipante = con.query(SQL_INSERT_PARTICIPANT , [values], function (err, result) {
if (err) throw err;

var values_end = [
result.insertId, data.cep, data.estado, data.cidade, data.bairro, data.endereco, data.numero
]

if (result.affectedRows > 0 ) {
const insertEndPart = con.query(SQL_INSERT_ADDRESS_PARTICIPANT , [values_end], function(err, result2 ) {
if (err) throw err;

console.log('Number of records inserted in ADDRESS_PARTICIPANT table: ' + result2.affectedRows);
console.log('insertId.: ' + result2.insertId)

if (result.affectedRows > 0 ) {
const insertInscricao = con.query(SQL_INSERT_INSCRIPTIONS, [values_ins], function(err, result3) {
console.log(`Inscription recorded! id: `+resul3.insertId)
})

}
})
}
})

}

最佳答案

您可以使用 MySQL 的 LAST_INSERT_ID 我假设每个表都有一个带有 auto_increment 选项的 primray 键列。

With no argument, LAST_INSERT_ID() returns a BIGINT UNSIGNED (64-bit) value representing the first automatically generated value successfully inserted for an AUTO_INCREMENT column as a result of the most recently executed INSERT statement. The value of LAST_INSERT_ID() remains unchanged if no rows are successfully inserted.

https://dev.mysql.com/doc/refman/8.0/en/information-functions.html#function_last-insert-id

然后您可以在 NodeJS 中使用这些 INSERT

INSERT INTO participant (nome_completo, tipo_funcionario, data_nascimento, sexo, unidade, cpf, email, telefone, telefone_emergencia) VALUES( <other columns> )

下面的插入将使用LAST_INSERT_ID()来获取participant.id

INSERT INTO endereco_participante (participant_id, cep, estado, cidade, bairro, endereco, numero) values( LAST_INSERT_ID(), <other columns> )

对于三个表,问题变得更加复杂。
然后就可以使用MySQL的用户变量了。

INSERT INTO participant (nome_completo, tipo_funcionario, data_nascimento, sexo, unidade, cpf, email, telefone, telefone_emergencia) VALUES( <other columns> )

SET @participant_id = LAST_INSERT_ID();

INSERT INTO endereco_participante (participant_id, cep, estado, cidade, bairro, endereco, numero) values( @participant_id, <other columns> )

SET @endereco_participante_id = LAST_INSERT_ID();

然后您可以在第三个插入查询中使用@participant_id和@endereco_participante_id。 (您在问题中没有提供这一点)。
请注意,SET 查询是单独的查询,因此您还需要使用 con.query('SET @participant_id = LAST_INSERT_ID();', ..)

执行它们

关于javascript - 使用 NodeJS 在 mysql 中插入关系表的最佳方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52407941/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com