Marron1
Marron1

Reputation: 23

MySQL sum up a column to a LIMIT and return sum value

I have a database and I'm trying to sum up a column up to a specific point with say, LIMIT, and then return the sum value, but it's not working. This is what I have:

<?php 
$servername = "localhost";
$username = "xxxxx";
$password = "xxxxxx";
$dbname = "xxxxxx";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die ("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT sum(donation_count) FROM (SELECT donation_count FROM users LIMIT 9) AS value_sum";

$result = $conn->query ($sql);  

if ($result-> num_rows> 0) {
    while ($row = $result->fetch_assoc()) {
        echo '<div>'".$row['value_sum']."'</div>';
    }
}
?>

Upvotes: 1

Views: 839

Answers (1)

Aldwen
Aldwen

Reputation: 338

SELECT sum(donation_count) as value_sum FROM (SELECT donation_count  FROM users LIMIT 9) AS temp

Upvotes: 1

Related Questions