perform subtraction and show the values between 1 and 10

4

I have a table which contains

I have this sentence to get me to subtract fewer exits:

$sql="SELECT medicina_inventario,fecha_inventario,entrada_inventario,salida_inventario,total_inventario,inveObservaciones, entrada_inventario-salida_inventario total_inventario FROM tblinventario";
$resultado=$objConexion->query($sql);

Now I need you to show only where the total of the subtraction between 1 and 10

I handle this but it does not give me the result:

$sql="SELECT medicina_inventario,fecha_inventario,entrada_inventario,salida_inventario,total_inventario,inveObservaciones, entrada_inventario-salida_inventario AS total FROM tblinventario 
UNION ALL 
SELECT  AS total FROM tblinventario WHERE total='10'";
    
asked by Valentina 09.11.2018 в 14:12
source

2 answers

4

If your fields are not aggregation (count, sum, avg, enter others) you can do it in the where:

SELECT entrada_inventario-salida_inventario AS total 
  FROM tblinventario 
  WHERE (entrada_inventario-salida_inventario) >=1 
    and (entrada_inventario-salida_inventario)<=10

In case you use aggregation functions I recommend using the HAVING that it would be a where within the query already finished and we can use the ALIAS, so you would avoid the UNION

SELECT entrada_inventario-salida_inventario AS total 
  FROM tblinventario 
 having total >=1 and total <=10
    
answered by 09.11.2018 / 14:28
source
2

I would recommend that at your WHERE you add the use of BETWEEN to indicate a range / interval of values you want to obtain; so that you would also be setting a limit of values that you want to show and discarding the others

The first value you place is where you should start from; in this case 1, the second value in this case 10 is up to where it should end

CODE

$sql="SELECT medicina_inventario, fecha_inventario, entrada_inventario, 
             salida_inventario, total_inventario, inveObservaciones, 
             entrada_inventario-salida_inventario AS total 
      FROM tblinventario 
      UNION ALL 
      SELECT AS total FROM tblinventario 
      WHERE total= BETWEEN 1 AND 10";
    
answered by 09.11.2018 в 14:25