This is my query. So i need to retrieve the sum from the values of budget meta key which are only in component 1 and in year 2009. How do i do this. Thanks in advance
SELECT SUM(
b.meta_value)
, b.meta_key
FROM wp_posts AS p, wp_postmeta AS b, wp_postmeta AS m, wp_postmeta AS n
WHERE (
p.ID = b.post_id
)
AND (
b.meta_key = 'budget'
)
AND (
m.meta_key = 'component'
AND m.meta_value = '1'
)
AND (
n.meta_key = 'component-year'
AND n.meta_value = '2009'
)
OK. So I think what you are saying is that you have posts with three custom fields; “component”, “component-year”, and “budget”. You are trying to get a total of values of “budget” when component=1 and component-year=2009. If that is correct, then this works – it is not the most efficient but it works fine.