简体   繁体   English

用于检索 WooCommerce 订单项和元数据的 SQL 选择查询

[英]SQL Select Query for Retrieving WooCommerce Order Items and Meta Data

I'm working on creating some SQL Views for my Wordpress database to more easily view online orders generated via the WooCommerce plugin.我正在为我的 Wordpress 数据库创建一些 SQL 视图,以便更轻松地查看通过 WooCommerce 插件生成的在线订单。 I'm familiar with the WooCommerce structure and where the data is stored but being an SQL novice I'm getting stuck at the last hurdle.我熟悉 WooCommerce 结构以及数据的存储位置,但作为 SQL 新手,我遇到了最后一个障碍。

I've been able to create the first view of all the order details using this reference:我已经能够使用此参考创建所有订单详细信息的第一个视图:

http://codecharismatic.com/sql-script-to-get-all-woocommerce-orders-including-metadata/ http://codecharismatic.com/sql-script-to-get-all-woocommerce-orders-including-metadata/

Now I need to create a similar view of the line items for each of these orders.现在,我需要为每个订单创建类似的订单项视图。 There are 2 tables that store order item details:有 2 个表存储订单项目的详细信息:

wp_woocommerce_order_items wp_woocommerce_order_itemmeta wp_woocommerce_order_items wp_woocommerce_order_itemmeta

I've been able to create the basic list of records from the wp_woocommerce_order_items table as follows:我已经能够从 wp_woocommerce_order_items 表中创建记录的基本列表,如下所示:

SELECT order_item_id,order_item_name,order_item_type,order_id
FROM ocm_woocommerce_order_items 
ORDER BY order_id

This returns a nice list of records like this:这将返回一个很好的记录列表,如下所示:

order_item_id   order_item_name order_item_type order_id
 2  Widgets line_item   9
 3  Widgets line_item   10
 4  Widgets line_item   11
 5  Woo Logo    line_item   473
 6  Woo Logo    line_item   473

I would now like to add additional columns for each line item from the wp_woocommerce_order_itemmeta table.我现在想为 wp_woocommerce_order_itemmeta 表中的每个行项目添加额外的列。 This table contains multiple records for each line item like this:此表包含每个行项目的多条记录,如下所示:

 meta_id    order_item_id   meta_key    meta_value
 136    16  _qty    4
 137    16  _tax_class  
 138    16  _product_id 87
 139    16  _variation_id   0
 140    16  _line_subtotal  36
 141    16  _line_total 36
 142    16  _line_subtotal_tax  3.6
 143    16  _line_tax   3.6

I would like to convert each of these into a new column so my output would consist of:我想将其中的每一个转换为一个新列,以便我的输出包括:

 order_item_id, order_item_name, order_item_type, order_id, qty, tax_class, product_id, variation_id, line_subtotal, line_total, line_subtotal_tax, line_tax

I'm not sure which SQL syntax to use here - I've tried to use a similar syntax to the link above but haven't been able to get it to work so far and not sure if that is indeed the correct syntax.我不确定在这里使用哪种 SQL 语法 - 我已经尝试使用与上面链接类似的语法,但到目前为止还无法使其正常工作,并且不确定这是否确实是正确的语法。

I managed to work this out by trial and error.我通过反复试验设法解决了这个问题。 Here's the query that worked in the end:这是最终有效的查询:

select
p.order_id,
p.order_item_id,
p.order_item_name,
p.order_item_type,
max( CASE WHEN pm.meta_key = '_product_id' and p.order_item_id = pm.order_item_id THEN pm.meta_value END ) as productID,
max( CASE WHEN pm.meta_key = '_qty' and p.order_item_id = pm.order_item_id THEN pm.meta_value END ) as Qty,
max( CASE WHEN pm.meta_key = '_variation_id' and p.order_item_id = pm.order_item_id THEN pm.meta_value END ) as variationID,
max( CASE WHEN pm.meta_key = '_line_total' and p.order_item_id = pm.order_item_id THEN pm.meta_value END ) as lineTotal,
max( CASE WHEN pm.meta_key = '_line_subtotal_tax' and p.order_item_id = pm.order_item_id THEN pm.meta_value END ) as subTotalTax,
max( CASE WHEN pm.meta_key = '_line_tax' and p.order_item_id = pm.order_item_id THEN pm.meta_value END ) as Tax,
max( CASE WHEN pm.meta_key = '_tax_class' and p.order_item_id = pm.order_item_id THEN pm.meta_value END ) as taxClass,
max( CASE WHEN pm.meta_key = '_line_subtotal' and p.order_item_id = pm.order_item_id THEN pm.meta_value END ) as subtotal
from
ocm_woocommerce_order_items as p,
ocm_woocommerce_order_itemmeta as pm
 where order_item_type = 'line_item' and
 p.order_item_id = pm.order_item_id
 group by
p.order_item_id

@mostafa Norzade I don't have commenting previlige so posting as answer @mostafa Norzade 我没有评论 previlige 所以张贴作为答案

the meta_key for user_id is '_customer_user', so simply add the following: max( CASE WHEN pm.meta_key = '_customer_user ' and p.ID = pm.post_id THEN pm.meta_value END ) as customer_user, user_id 的 meta_key 是 '_customer_user',所以只需添加以下内容:max( CASE WHEN pm.meta_key = '_customer_user ' and p.ID = pm.post_id THEN pm.meta_value END ) as customer_user,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM