Учитывая схему таблиц, как показано ниже:
create schema simulado;
use simulado;
create table mo(
id integer Primary Key auto_increment,
groupTypologyId int,
typologyId int,
contractId varchar(45),
tradeDate datetime,
index (id)
);
create table eq(
id int,
typologyId int,
typologyName varchar(20),
delta decimal,
index(typologyId),
constraint fk_ideq foreign key (id) references mo (id)
);
create table cm(
id int,
typologyId int,
typologyName varchar(20),
shortleg decimal,
index(typologyId),
constraint fk_idcm foreign key (id) references mo (id)
);
create table ir(
id int,
typologyId int,
typologyName varchar(20),
isin varchar(20),
index(typologyId),
constraint fk_idir foreign key (id) references mo (id)
);
create table fx(
id int,
typologyId int,
typologyName varchar(20),
dma bit,
index(typologyId),
constraint fk_idfx foreign key (id) references mo (id)
);
alter table mo
add constraint fk_eq foreign key (typologyId) references eq(TypologyId),
add constraint fk_fx foreign key (typologyId) references fx(TypologyId),
add constraint fk_ir foreign key (typologyId) references ir(TypologyId),
add constraint fk_cm foreign key (typologyId) references cm(TypologyId);
Мне нужно вставить данные таким образом, чтобы из значений столбца groupTypologyId
на mo
данные были взяты на столбцы, ссылающиеся на groupTypologyId
. Например, groupTypologyId = 1
относится к таблице eq
, а groupTypologyId = 2 refers
к таблице fx
.
Mo Table
id | groupTypologyId | typologyId | contractId | tradeDate
1 | 1 | 1 | xxxxxxxxxx | 03/04/2020
2 | 2 | 5 | yyyyyyyyyy | 03/02/2020
Eq Table
id | TypologyId | TypologyName | Delta
1 | 1 | Eq A | 0.5
Fx Table
id | TypologyId | TypologyName | dma
2 | 5 | Fx A | 0
Итак, я хотел бы создать команду, которая анализирует значение groupTypolyId в mo, и из этого анализа направьте TypologyId в таблицу, ссылаясь на ее типологию, а затем выполните еще одну вставку с указанными c столбцами каждой таблицы. Возможно ли это сделать в MySQL? Я безуспешно пытаюсь выполнить команду ниже, которая пытается вставить Id вручную, что, даже если он был успешным, я не хочу этого делать.
use simulado;
insert into mo values (1,1,1,'xxxx', now());
select Last_Insert_Id() into @gp;
insert into eq values(@gp, 1, 'eq a', 0.5);