1
Follow my Mysql code:
create schema if not exists Faculdade;
use faculdade;
create table if not exists Alunos
(
RA integer UNSIGNED not null,
senha varchar(15) not null,
nome varchar(100) not null,
faltas integer,
PRIMARY KEY (RA)
)ENGINE = InnoDB;
create table if not exists Materias
(
id SMALLINT UNSIGNED NOT NULL,
nome varchar(50) not null,
nota1 float,
nota2 float,
medias float,
substitutiva float,
RA integer UNSIGNED not null,
primary key(id,RA)
)ENGINE = InnoDB;
ALTER TABLE `Materias` ADD CONSTRAINT `RA` FOREIGN KEY ( `RA` ) REFERENCES `Alunos` ( `RA` ) ;
drop database faculdade;
describe Alunos;
describe Materias;
insert into Alunos values(201500983,'12345','Marcos Paes Leme',0);
insert into Alunos values(201500984,'54321','Dionizio',0);
insert into Alunos values(201500985,'1357','Paulo José',0);
insert into Alunos values(201500986,'2468','Robervau',0);
select * from Alunos;
insert into Materias values(1,'Alged',6.0,7.0,(select sum(nota1+nota2)/2 from Materias where RA = 201500984),0.0,(select RA from Alunos where RA=201500984));
insert into Materias values(1,'Alged',6.0,7.0,(select sum(nota1+nota2)/2 from Materias where RA = 201500984),0.0,(select RA from Alunos where RA=201500984));
select * from Materias;
When I try to execute the insert
on my table materias
of the following error:
you can’t specify target table for update in from clause mysql.
When I run the command separately: select sum(nota1+nota2) from Materias where RA = 201500984
he executes.
Did any of the answers solve your problem? Do you think you can accept one of them? If you haven’t already, see [tour] how to do this. You would help the community by identifying the best solution for you. You can only accept one of them, but you can vote for any question or answer you find useful on the entire site (if you have enough score).
– Maniero