Storing the value of a SELECT Query in a column in MYSQL

Asked

Viewed 42 times

1

I have this SELECT in my BD however I need one of the columns to store the value generated by lucro, I already have a profit column created as DOUBLE but it does not store that information

SELECT that I’m wearing:

 SELECT pac.ID_Pacote, pro.Nome, pac.peca, pac.FK_ID_Produto, 
        pac.Quantidade, pac.Data_entrada, pac.Data_saida,
        CONCAT('R$ ', ROUND(pac.Quantidade * pro.Preco, 2)) AS Lucro 
   FROM pacote pac
  INNER
   JOIN produto pro
     ON pac.FK_ID_Produto = pro.ID_Produto

Bank code

CREATE DATABASE ProdPacote;
USE ProdPacote;

CREATE TABLE Produto(
ID_Produto INT PRIMARY KEY AUTO_INCREMENT,
Nome VARCHAR (200) NOT NULL,
Descricao VARCHAR (200) NOT NULL,
Preco DOUBLE NOT NULL,
`status` CHAR (10) NOT NULL);  

CREATE TABLE Pacote(
ID_Pacote INT PRIMARY KEY AUTO_INCREMENT,
Nome VARCHAR (200) NOT NULL,
peca VARCHAR (200) NOT NULL,    
FK_ID_Produto int NOT NULL,    
Quantidade INT NOT NULL,
Data_entrada DATE NOT NULL,
Data_saida DATE,
Lucro DOUBLE,
CONSTRAINT fk1
FOREIGN KEY (FK_ID_Produto)
REFERENCES Produto(ID_Produto));

1 answer

2

From what I understand you want a column called profit, present in your table receive the calculation you are doing in SELECT for the alias Profit.

CONCAT('R$ ', ROUND(pacote.Quantidade * produto.Preco, 2)) AS Lucro

You must make this change at the time of entering the information, now it is a table update use an "INSERT INTO SELECT" to update the records.

If the column you want to update is in another table, use as an example:

INSERT INTO Lucros (lucro)
SELECT CONCAT('R$ ', ROUND(pacote.Quantidade * produto.Preco, 2)) AS Lucro 
FROM pacote
INNER JOIN produto ON pacote.FK_ID_Produto = produto.ID_Produto;
WHERE Lucros.id_produto = produto.ID_Produto;
  • so calm down, I would have to do this insert into the Quary of my correct project ?

  • I also put the code of the bank because it is generating some errors of a table Profit.Prodpackage does not exist (what in BD is not to have)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.