Title: SKU in SQL statement
Last modified: March 8, 2021

---

# SKU in SQL statement

 *  Resolved [mosabua](https://wordpress.org/support/users/mosabua/)
 * (@mosabua)
 * [5 years, 2 months ago](https://wordpress.org/support/topic/sku-in-sql-statement/)
 * I have a custom plugin from a developer that is not available anymore and he 
   wrote the following SQL query and now I should add the _sku to it. But I struggle
   to get it with all the left joins … So right now I get a result-list with product_id,
   post_title, date and quantity and now the resultlist should include the sku
 * The SQL:
 *     ```
       SELECT PD.ID as product_id, PD.post_title, ddo.date, SUM(ddo.quantity) as quantity FROM dckk0_daily_orders ddo 
               INNER JOIN 
               ( SELECT p.ID, p.post_title FROM dckk0_posts p LEFT JOIN dckk0_postmeta pm1 ON pm1.post_id = p.ID WHERE p.post_type in('product', 'product_variation') AND p.post_status = 'publish' GROUP BY p.ID,p.post_title ) AS PD ON PD.ID = ddo.product_id 
               INNER JOIN dckk0_posts p
               ON p.ID = ddo.order_id
               WHERE p.post_status in ('wc-pending', 'wc-processing', 'wc-on-hold', 'wc-completed') AND ddo.date >= DATE_FORMAT(CURDATE(),'%Y-%m-%d')
               GROUP BY PD.post_title, ddo.date
               ORDER BY ddo.date
       ```
   
 * Thanks and any help would be awesome werner

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Plugin Support [Gabriel – a11n](https://wordpress.org/support/users/gabrielfuentes/)
 * (@gabrielfuentes)
 * [5 years, 2 months ago](https://wordpress.org/support/topic/sku-in-sql-statement/#post-14150253)
 * Hi there 👋
 * This is a fairly complex development topic. I’m going to leave it open for a 
   bit to see if anyone is able to chime in to help you out.
 * I can also recommend [the WooCommerce Developer Resources Portal](https://developer.woocommerce.com/)
   for resources on developing for WooCommerce.
 * You can also visit the [WooCommerce Facebook group](https://www.facebook.com/groups/advanced.woocommerce/)
   or the `#developers` channel of the [WooCommerce Community Slack](https://woocommerce.com/community-slack/).
   We’re lucky to have a great community of open-source developers for WooCommerce,
   and many of our developers hang out there, as well.
 * Cheers!
 *  Thread Starter [mosabua](https://wordpress.org/support/users/mosabua/)
 * (@mosabua)
 * [5 years, 2 months ago](https://wordpress.org/support/topic/sku-in-sql-statement/#post-14160182)
 * Thanks for your links – after checking and re-thinking everthing I had an easier
   idea. I already have the id so I just used
    get_post_meta( $result->product_id,‘
   _sku’, TRUE) in the loop and it works like a charm.
 *  Plugin Support [Gabriel – a11n](https://wordpress.org/support/users/gabrielfuentes/)
 * (@gabrielfuentes)
 * [5 years, 2 months ago](https://wordpress.org/support/topic/sku-in-sql-statement/#post-14170160)
 * That’s wonderful! 🎉
 * Great! If you have any further questions, you can start a new thread.
 * Cheers!

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘SKU in SQL statement’ is closed to new replies.

 * ![](https://ps.w.org/woocommerce/assets/icon.svg?rev=3234504)
 * [WooCommerce](https://wordpress.org/plugins/woocommerce/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/woocommerce/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/woocommerce/)
 * [Active Topics](https://wordpress.org/support/plugin/woocommerce/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/woocommerce/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/woocommerce/reviews/)

## Tags

 * [sql](https://wordpress.org/support/topic-tag/sql/)

 * 3 replies
 * 2 participants
 * Last reply from: [Gabriel – a11n](https://wordpress.org/support/users/gabrielfuentes/)
 * Last activity: [5 years, 2 months ago](https://wordpress.org/support/topic/sku-in-sql-statement/#post-14170160)
 * Status: resolved