tags:

views:

92

answers:

2

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 :)

+12  A: 

Isn't it as simple as:

select sum(meta_value) from (table) where meta_key = 'views'
Andrew
Yes, it is that simple :)
James Skidmore
+4  A: 

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

Myles