我正在尝试在我的Woocommerce商店运行一个SQL查询,该商店输出购买了一组商品和订购数量的所有客户名称(第一个和最后一个)。架构是标准的Wordpress / Woocommerce。我的第一个查询(下面)有效,但没有输出订购数量:
SELECT `meta_value`, `post_id`, `order_item_name`
FROM `wp_postmeta` AS pm
JOIN wp_woocommerce_order_items AS oi ON pm.post_id = oi.order_id
WHERE `order_item_name` LIKE 'Photo%' AND `meta_key` LIKE '%shipping%name'
尝试将数量添加到查询时,我不断收到不明确的列错误Column 'meta_value' in field list is ambiguous
。尝试以各种组合向表/列添加别名时,我会收到未知的列错误Unknown column 'wp_postmeta.meta_value' in 'field list'
。这是我更新的查询:
SELECT `meta_value` , `post_id` , `order_item_name`, `om.meta_key`
FROM `wp_postmeta` AS pm
JOIN wp_woocommerce_order_items AS oi ON pm.post_id = oi.order_id
JOIN wp_woocommerce_order_itemmeta as om ON pm.order_item_id=om.order_item_id
WHERE `order_item_name` LIKE 'Photo%'
AND `pm.meta_key` LIKE '%shipping%name'
AND `om.meta_key` LIKE '_qty'
答案 0 :(得分:0)
在您的案例中使用别名pm
时,您也不能使用表名wp_postmeta
使用
SELECT `pm`.`meta_value`, `post_id`, `order_item_name`
FROM `wp_postmeta` AS pm
JOIN wp_woocommerce_order_items AS oi ON pm.post_id = oi.order_id
WHERE `order_item_name` LIKE 'Photo%' AND `meta_key` LIKE '%shipping%name'
取代所需的结果。