1

How would I add up all the integers in the column, _view_count_, on my table, 'videos', then echo it to display on my page?

For example:

if row id 1 has view_count == 328

and

if row id 2 has view_count == 271

How would I make MySQL add those together and echo it out?

4 Answers 4

2

You can use MySQL's SUM() and your SQL query would look something similar to:

SELECT SUM(view_count) FROM videos;

To query and echo the value, if you're using mysqli methods in your PHP code, you can use:

$result = $mysqli->query('SELECT SUM(view_count) AS sum FROM videos;');
$row = $result->fetch_assoc($result);
echo $row['sum'];
Sign up to request clarification or add additional context in comments.

Comments

0

Assuming you have a $mysqli object defined your code should look like:

$query = "SELECT Sum(view_count) as mySum FROM videos";
$result = $mysqli->query($query);
$row = $result->fetch_assoc($result);
echo $row['mySum'];

2 Comments

As the mysql_ functions are no longer recommended for use, please do not recommend the OP to use them - especially when they do not indicate their usage in their question.
That is correct mysql is now deprecated, lookup the equivalent functions in the mysqli libraries.
0

SELECT SUM(view_count) FROM videos

Comments

0

Assuming you have a COLUMN id, you can use SUM together with IN, like so:

SELECT SUM(view_count) FROM videos WHERE id in (1,2)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.