Help with this query

0

I am bringing information from several tables for a general information report based on the date but the problem is that I duplicate information

    SELECT insumos.fecha,insumos.producto,insumos.cantidad,
 mano_obra.nombre,mano_obra.pago,mano_obra.fecha,
 mantenimiento.descripcion,mantenimiento.gasto,mantenimiento.fecha,
 combustible.vehiculo,combustible.gasolina,combustible.fecha FROM
 insumos,mano_obra,mantenimiento,combustible WHERE insumos.fecha 
between '2018-01-01' AND '2018-01-01' and mano_obra.fecha 
between '2018-01-01' AND '2018-01-01' and mantenimiento.fecha 
between '2018-01-01' AND '2018-01-01' and combustible.fecha 
between '2018-01-01' AND '2018-01-01'

result:

Example of how it should be:

Someone who can help me with this, I would really appreciate it.

    
asked by DoubleM 01.02.2018 в 01:01
source

1 answer

0

Your problem is the way you are grouping the tables.

... FROM tableA, tableB ...

What is happening is SQL performs a combination, so for every record in table B it relates all the records in table A. That is why they duplicate you.

If the tables have the same structure (which I do not believe), you could use the UNION clause, for example:

CREATE TABLE PERSONAS (
  ID VARHCAR(10),
  NAME VARCHAR(50)
);

CREATE TABLE VEHICULOS(
 ID VARCHAR(10),
 NAME VARHCAR(50)
);

With the structure of the two previous tables you could do the following:

 SELECT * FROM PERSONAS 
 UNION 
 SELECT * FROM VEHICULOS;

The other solution is if your tables are related, if they have one, you can use INNER JOIN.

    
answered by 01.02.2018 в 02:54