Display data from a table summing a specific column

Asked

Viewed 30 times

0

good afternoon!

I’m a beginner in php and I’m trying to build a table that tells the date and the sum of sales made, however, I can only present the sum of the values but I can not present the date mentioned... follow Cód.

$mysqli = new MySQLi( 'localhost', 'login', 'senha', 'db' );
$q_soma= ('SELECT SUM(valor) AS resultado FROM vendas GROUP BY data');

if ($result = $mysqli->query($q_soma)) {
    while ($row = $result->fetch_assoc()) {
        printf ("%s <br />", $row["resultado"]);
    }
    $result->close();
};
  • You need to put the date field in the field list and then print as you did with resultado

1 answer

0

$q_soma= ('SELECT SUM(value) AS result FROM sales GROUP BY data');

Switch to:

$q_soma= ('SELECT SUM(value) AS result, date FROM sales GROUP BY data');

With all the changes made, you get something like this:

$mysqli = new MySQLi( 'localhost', 'login', 'senha', 'db' );
$q_soma= ('SELECT SUM(valor) AS resultado FROM vendas GROUP BY data');

if ($result = $mysqli->query($q_soma)) {
    while ($row = $result->fetch_assoc()) {
        print "vendas: {$row['resultado']} - {$row['data']}";
    }
    $result->close();
};

Browser other questions tagged

You are not signed in. Login or sign up in order to post.