SQL Select Sum Statement To Total Orders By Another Custom Field?
-
Hi Guys, quick question as I’m not very good with SQL. The wp_postmeta table has four fields: post_id, meta_id, meta_key, meta_value. I need an SQL statement that sums information for one meta value (found by meta key) based on searching for another meta key/value pair. The only thing that is common between the meta_key’s is the post_id. I can probably figure out how to do it in 2 calls to the DB, but wanted to condense it to a single call if possible?
Here’s basically the 2 step process I’m looking to condense:
$array_of_post_ids = $wpdb->get_col("SELECT post_id FROM cb_postmeta WHERE 'meta_key' = 'user_id' AND 'meta_value' = <actual user ID>"); Then you'd just loop through the array... $total; foreach($array_of_post_ids as $id) { $amount = $wpdb->get_var("SELECT '_money_spent' FROM cb_postmeta WHERE 'post_id' = $id"); }My brain just goes numb when I try to figure how to condense stuff like this, but is there a way to shorten it so it only makes one call? Should I use a join statement?
The topic ‘SQL Select Sum Statement To Total Orders By Another Custom Field?’ is closed to new replies.