我目前正在开发一个专为面包店设计的插件,用于通过电子邮件发送有关订单的报告。他们已经开设了另一家商店,现在需要根据商店位置将报告拆分为单独的电子邮件。
我有以下sql查询:
$sql = "SELECT o.order_item_id, o.order_item_name, o.order_id, po.post_date AS order_date_placed
FROM wp_woocommerce_order_items AS o
LEFT JOIN wp_woocommerce_order_itemmeta AS oi ON o.order_item_id = oi.order_item_id
LEFT JOIN wp_posts AS po ON po.ID = o.order_id
WHERE oi.meta_key='Collection Date'
AND oi.meta_value IN ('16/01/2016','17/01/2016','18/01/2016','19/01/2016','20/01/2016','21/01/2016')
ORDER BY po.post_date ASC, o.order_item_id ASC";
此查询返回在特定日期范围内收集的所有订单。
array (size=3)
0 =>
object(stdClass)[593]
public 'order_item_id' => string '19582' (length=5)
public 'order_item_name' => string '10 Chocolate Cupcakes' (length=21)
public 'order_id' => string '15234' (length=5)
public 'order_date_placed' => string '2016-01-14 08:51:31' (length=19)
1 =>
object(stdClass)[592]
public 'order_item_id' => string '19600' (length=5)
public 'order_item_name' => string '10 Chocolate Cupcakes' (length=21)
public 'order_id' => string '15248' (length=5)
public 'order_date_placed' => string '2016-01-14 15:43:08' (length=19)
2 =>
object(stdClass)[591]
public 'order_item_id' => string '19603' (length=5)
public 'order_item_name' => string '10 Chocolate Cupcakes' (length=21)
public 'order_id' => string '15250' (length=5)
public 'order_date_placed' => string '2016-01-14 15:45:25' (length=19)
我现在想要传递一个额外的标准,该标准也会将返回数据限制在特定的商店位置。
我尝试在查询中添加以下内容,但这会返回null。
WHERE oi.meta_key = 'Pickup Location'
AND oi.meta_value LIKE '%Address 1%'
只有两个条件都符合的记录必须返回。
我确实很难理解为什么wordpress / woocommerce中的meta表以他们拥有如此多冗余/重复条目的方式设置。
我不确定开发人员是否应该使用原始SQL查询,或者是否有更好的解决方案来解决此问题。
非常感谢任何帮助。
答案 0 :(得分:0)
我猜您正在寻找此查询
SELECT o.order_item_id,
o.order_item_name,
o.order_id,
po.post_date AS order_date_placed
FROM wp_woocommerce_order_items AS o
LEFT JOIN wp_woocommerce_order_itemmeta AS oi
ON ( o.order_item_id = oi.order_item_id
AND oi.meta_key = 'Collection Date'
AND oi.meta_value IN ( '16/01/2016', '17/01/2016','18/01/2016',
'19/01/2016','20/01/2016', '21/01/2016' ) )
OR ( oi.meta_key = 'Pickup Location'
AND oi.meta_value LIKE '%Address 1%' )
LEFT JOIN wp_posts AS po
ON po.id = o.order_id
ORDER BY po.post_date ASC,
o.order_item_id ASC
并将过滤器移至ON
条件,因为它们属于右表