我目前正在开发一个为面包店构建的插件,用于通过电子邮件发送有关订单的报告。从那以后,他们就开了另一家商店,现在,根据商店的位置,需要将报告分成单独的电子邮件。

我有以下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中的元表是按照具有这么多冗余/重复条目的方式来设置的。

我不确定开发人员是否应该使用原始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
                 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条件,因为它们属于右表

关于php - Woocommerce选择匹配多个元值的订单,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34805391/

10-16 18:50