how to join two tables in another table [duplicated]

0

My problem is that I have two table input and output is related to another table inventory , I want it to show the products of the entries in my table inventory and output in my inventory table this is my query

select idinventario,i.identrada,(select nombre from entrada 
where identrada=i.identrada)as nombre,
i.idsalida, (select nombre from salida where idsalida=i.idsalida)as nombre1,
i.idusuario,(select login from usuario where idpersona=i.idusuario)as 
usuarios,   fecha_inventario,fecha_ingreso,fecha_salida,observacion from
inventario i inner join entrada e on i.identrada=e.identrada,inner join 
salida s on i.idsalida=s.idsalida;  
    
asked by Yusmary Valerio 29.08.2018 в 22:50
source

1 answer

1

The first thing you need to do is make a structure that has the same columns in the two tables, for example:

  • id (exit or entry id according to the source table)
  • name
  • date (departure date if it is in the exit or entry table if it is in the other)
  • observations
  • user
  • type (this is important, you have to put a fixed text 'input' or 'output' in the specified selection)

Then you just have to make the two consultations respecting this structure and use the UNION function

Here is the manual: link

I leave you a mini example:

select idsalida as id, fecha_salida as fecha, observacion, 'salida' as tipo from salidas
UNION
select identrada as id, fecha_entrada as fecha, observacion, 'entrada' as tipo from entrada

I hope it serves you.

    
answered by 30.08.2018 в 00:30