0

I have a table like this (using wordpress)

+---------+----------+------------+
| meta_id | meta_key | meta_value |
+---------+----------+------------+
|   1     |   views  |     3      |
|   2     |   blahh  |  a value   |
|   3     |   smthn  |    boo     |
|   4     |   views  |     4      |
|   5     |   views  |     5      |
|   6     |   views  |     6      |
|   7     |   views  |     7      |
|   8     |   views  |     8      |
+---------+----------+------------+

So I want to SELECT everything WHERE the meta_key = 'views' then take the number from meta_value then add up all those numbers.

I'm not sure if this is possible, if not I could use PHP to add everything up. I thought it would be interesting to know if I can add things in SQL :)

2 Answers 2

12

Isn't it as simple as:

select sum(meta_value) from (table) where meta_key = 'views'
Sign up to request clarification or add additional context in comments.

Comments

4

You should be able to do something like SELECT SUM(meta_value) as total FROM <table name> WHERE meta_key = 'views'

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.