How do I add the data that a field of a table contains?

0

Greetings, I need to generate a daily income report, the consultation is not my problem because I would have to select the amounts of the payment table where the payment date is today. My problem is that to make that daily income report I have to add all the amounts that meet that condition. These are the tables where those amounts are:

Of these two tables I have to bring those amounts, which is not how to add all the amounts where the payment date is today. Thanks to whoever guides me

    
asked by Alejo Mendoza 29.08.2017 в 04:19
source

2 answers

0
$conn = new mysqli("localhost", "root", "", 'nombre_bd');
$montoshoy;
if (!$conn) {
    die("Conexion con MySQL invalida: " . mysqli_connect_error());
}
else{
  echo "Conexion con MySQL correcta";
}

    $sql = "SELECT monto FROM tabla where fecha=NOW()";
    $result = $conn->query($sql);
if ($result->num_rows > 0) {

while($row = $result->fetch_assoc()) {
    $montoshoy+=intval($row['monto']);
}
}
$conn->close();

The code would be something like that. The only question is whether the NOW () would work but it should be a PHP date (y-m-d). Greetings.

    
answered by 29.08.2017 / 04:30
source
0

You can use the aggregation function SUM to obtain the total amount, and using CURDATE (which returns the current date in YYY-MM-DD format) filter the result so that it only sums the day's payments.

SELECT SUM(monto) FROM pago_estudiantes WHERE fecha_del_pago_hecho = CURDATE();

Also, if necessary you can use the DATE_FORMAT function to format a date according to the specified mask.

SELECT SUM(monto) FROM pago_estudiantes
WHERE fecha_del_pago_hecho = DATE_FORMAT(CURDATE(), "%d-%m-%y");
    
answered by 29.08.2017 в 04:51