Doubt with Query SQL

Asked

Viewed 81 times

1

I have a stock movement table with the following columns and data:

table moves

id | operacao | prodinsumo | qtde
----------------------------------
1  | 1        | 25         | 6
2  | 2        | 10         | 3
3  | 10       | 17         | 1
4  | 11       | 10         | 2

table inputs:

codinsumo | nomeinsumo
------------------------
10        | água mineral

table products:

codproduto | nomeproduto
---------------------------    
25         | refrigerante
17         | espetinho

In the column operacao are stored the codes of the operations that are:

1 - Product sale

2 - Sale Insumo

10 - Estorno Product

11 - Estorno Insumo

The column prodinsumo stores the input code or the product according to the operation.

I would like to be able to do a query that puts the name of the input or product according to the operation, but I do not know how to do.

Here is a query I made that serves to select the columns but does not decide which table will get the name, whether it is of inputs or products.

select
case m.operacao
  when 1 then "Venda produto"
  when 2 then "Venda insumo"
  when 10 then "Estorno produto"
  when 11 then "Estorno insumo"
end as operacao,
i.nomeinsumo as nome, -- ou p.nomeproduto as nome
m.qtde
from movimentacoes m
left join produtos p on m.prodinsumo = p.codproduto
left join insumos i on m.prodinsumo = i.codinsumo

I need that when the column operacao have the values 1 or 10 the column nome either with the column value nomeproduto table produtos and when you have the values 2 or 11 either with the column value nomeinsumo table insumos.

  • Put your schema in sqlfiddle, so help us already send you the ready query. But finally, to use this logic, you can use https://dev.mysql.com/doc/refman/5.7/en/case.html when the value is x, make a subselect, if not another.

2 answers

1

Try using SQL below, remembering that the select result of the name should always return only 1 value, so of LIMIT 1.

select
case m.operacao
  when 1 then "Venda produto"
  when 2 then "Venda insumo"
  when 10 then "Estorno produto"
  when 11 then "Estorno insumo"
end as operacao,
case m.operacao
  when 1 then (select nomeproduto from produtos where codproduto = 1  LIMIT 1)
  when 2 then (select nomeinsumo from insumos where codinsumo = 2  LIMIT 1)
  when 10 then (select nomeproduto from produtos where codproduto = 10  LIMIT 1)
  when 11 then (select nomeinsumo from insumos where codinsumo = 11  LIMIT 1)
end as nome,
m.qtde
from movimentacoes m
left join produtos p on m.prodinsumo = p.codproduto
left join insumos i on m.prodinsumo = i.codinsumo
  • Friend, I voted in favour of your answer because it helped me to solve the problem, I did it in a different way that did not need to do the subselects with the LIMIT 1.

0


After the reply of @Pedroe. I managed to do the query below that solved the problem in a better way.

This query is taking into account that when the operations of the products are not found the operations will be inputs, because there are no other operations than those that are in the question.

select
case m.operacao
  when 1 then "Venda produto"
  when 2 then "Venda insumo"
  when 10 then "Estorno produto"
  when 11 then "Estorno insumo"
end as operacao,
case m.operacao
  when 1 then p.nomeproduto
  when 10 then p.nomeproduto
  else i.nomeinsumo
end as nome, 
m.qtde
from movimentacoes m
left join produtos p on m.prodinsumo = p.codproduto
left join insumos i on m.prodinsumo = i.codinsumo

Browser other questions tagged

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