WPDB class – what are the 'key1, key2, key3' used in JOIN Statements?
-
Hi Guys,
So I’m toying around with inner joins using the $wpdb class to pull data from woocommerce tables: woocommerce_order_items and woocommerce_order_itemmeta tables, but I’m really confused as to what all of the code in the wpdb reference means. There are two values in the itemmeta table that I need – the total cost of an order and the fundraisers id – both are meta keys in the meta_key column of the itemmeta table.
Here is the example code they give you. If someone could take me step by step through the code below, I’d be REALLY, REALLY, HUGELY grateful ?? :
$meta_key1 = 'model'; $meta_key2 = 'year'; $meta_key3 = 'manufacturer'; $meta_key3_value = 'Ford'; $postids=$wpdb->get_col( $wpdb->prepare( " SELECT key3.post_id FROM $wpdb->postmeta key3 INNER JOIN $wpdb->postmeta key1 ON key1.post_id = key3.post_id AND key1.meta_key = %s INNER JOIN $wpdb->postmeta key2 ON key2.post_id = key3.post_id AND key2.meta_key = %s WHERE key3.meta_key = %s AND key3.meta_value = %s ORDER BY key1.meta_value, key2.meta_value ", $meta_key1, $meta_key2, $meta_key3, $meta_key3_value ) );
The biggest thing that’s throwing me off is code like this:
$wpdb->postmeta key3
From the looks of it, the $wpdb OBJECT is accessing the postmeta table in the wordpress database. So what exactly is the ‘key3’ reference after it? Is key3 a new reference to $wpdb->postmeta?Thanks for shedding some light on this for me!!! I REALLY APPRECIATE YOUR HELP!!! ??
- The topic ‘WPDB class – what are the 'key1, key2, key3' used in JOIN Statements?’ is closed to new replies.