I have the following tables where I'm taking an inventory, but I also have a reservation table.
Tabla Inventario
-------------------
| pieza |cantidad |
|--------|---------|
| pieza1 |100 |
| pieza2 |20 |
| pieza3 |30 |
| pieza4 |10 |
| pieza5 |30 |
|--------|---------|
Tabla Reserva
-------------------
| pieza |cantidad |
|--------|---------|
| pieza1 |10 |
| pieza1 |20 |
| pieza2 |10 |
| pieza2 |5 |
| pieza5 |30 |
|--------|---------|
I have the following query where I make a query if the piece has a reservation.
$result_reserva = $conexion->query("SELECT * FROM Inv_Reserva
where CodPza='$codpza'");
if($res_reserva=$result_reserva->fetch_assoc())
{
$reserva="SI";
$query = "SELECT SUM(Cantidad) FROM Inv_Reserva where CodPza='$codpza' ";
$result = mysqli_query($conexion, $query);
$row = mysqli_fetch_row($result);
$reservatotal = $row[0];
$query2 = "SELECT Cantidad FROM Inv_Inventario where CodPza='$codpza'";
$result2 = mysqli_query($conexion, $query2);
$roww = mysqli_fetch_row($result2);
$invTotal = $roww[0];
$total = $reservaTotal-$invTotal;
}
else{
$reserva="NO";
}
If you find me I would like to make a query where I get the visible inventory available (Cantidad Inventario - Cantidad Reserva)
.
I have the following query to get the total sum of the reservations.
//Query Reserva total
SELECT SUM(Cantidad) FROM 'Inv_Reserva' WHERE CodPza='$pieza' ;
//Query Inventario
SELECT Cantidad FROM Inv_Inventario WHERE CodPza='$pieza';
But I would like to be able to do the addition and subtraction in a single query. I hope you have explained me well. Greetings!