CASE语句以及如何使用它的值

时间:2016-01-19 17:13:03

标签: mysql sql select case

我有一个查询

select 
    p.ID as order_id,
    p.post_date,
    i.order_item_id,
    max( CASE WHEN im.meta_key = '_product_id' and i.order_item_id = im.order_item_id THEN im.meta_value END ) as product_id,
    max( CASE WHEN im.meta_key = '_qty' and i.order_item_id = im.order_item_id THEN im.meta_value END ) as qty,   
    max( CASE WHEN prm.meta_key = '_sku' and im.meta_value = prm.post_id THEN prm.meta_value END ) as sku,
    max( CASE WHEN prm.meta_key = '_regular_price' and im.meta_value = prm.post_id THEN prm.meta_value END ) as product_price
from 
    wp_posts as p,
    wp_postmeta as pm,
    wp_woocommerce_order_items as i,
    wp_woocommerce_order_itemmeta as im,
    wp_postmeta as prm    
where 
    p.post_type = 'shop_order'
    and p.ID = pm.post_id
    and p.ID = i.order_id
    and p.post_date BETWEEN '2016-01-14 00:00:00' AND '2016-01-14 23:59:59'
    and p.post_status = 'wc-processing'
    and p.ID = i.order_id
    and i.order_item_id = im.order_item_id
group by
    p.ID  

但我仍然坚持使用sku获取product_priceproduct_id(两个值都为NULL),在phpMyAdmin上我将按其值搜索我可以获得SKU和产品价格meta_value以及位于meta_key表中的wp_postmeta的其他值。

根据以下评论和答案中的有用指示,我已经JOIN版本查询有效,但速度很慢:

select 
    p.ID as order_id,
    p.post_date,
    i.order_item_id,
    max( CASE WHEN im.meta_key = '_product_id' THEN im.meta_value END ) as product_id,
    max( CASE WHEN imq.meta_key = '_qty' THEN imq.meta_value END ) as qty,
    max( CASE WHEN prm.meta_key = '_sku' THEN prm.meta_value END ) as sku,
    max( CASE WHEN prm.meta_key = '_regular_price' THEN prm.meta_value END ) as product_price
from 
    wp_posts as p
join  
    wp_postmeta as pm
on
    p.ID = pm.post_id
join
    wp_woocommerce_order_items as i
on 
    p.ID = i.order_id
join
    wp_woocommerce_order_itemmeta as im
on
    i.order_item_id = im.order_item_id
join
    wp_woocommerce_order_itemmeta as imq
on
    i.order_item_id = imq.order_item_id
join  
    wp_postmeta as prm
on
    im.meta_value = prm.post_id
where 
    p.post_type = 'shop_order'
    and p.post_date BETWEEN '2016-01-14 00:00:00' AND '2016-01-14 23:59:59'
    and p.post_status = 'wc-processing'
group by
    p.ID  

我的问题是为什么我需要为每个JOIN语句创建CASE - 示例qty的其他方式将值返回为NULL。

2 个答案:

答案 0 :(得分:1)

prm(查询中的别名JOIN)的联接中似乎没有任何谓词。)因此,这是一个交叉连接操作。执行交叉连接是有效的;但是你想要的结果不太可能。

抛弃连接操作的旧式逗号语法。使用ANSI WHERE关键字。并将连接谓词从ON子句重定位到适当的CASE子句。

WHERE表达式中的某些条件测试是不必要的,因为它们是多余的。他们总是评价为真。

查询包含此连接谓词(在i.order_item_id = im.order_item_id 子句中)

CASE WHEN im.meta_key = '_product_id' and i.order_item_id = im.order_item_id THEN

同样的情况再次出现在CASE表达式中:

CASE WHEN im.meta_key = '_product_id' THEN

没有必要在CASE表达式中重复该条件,因为它会为返回的每一行计算为TRUE。

wp_postmeta

鉴于"缺失"将连接的谓词连接到prm(别名为im.meta_value = prm.post_id )...并给出我们在其他CASE表达式中看到的条件

SELECT p.id as order_id
     , p.post_date
     , i.order_item_id
     , MAX( CASE WHEN im.meta_key = '_product_id'     THEN im.meta_value  END ) as product_id
     , MAX( CASE WHEN im.meta_key = '_qty'            THEN im.meta_value  END ) as qty
     , MAX( CASE WHEN prm.meta_key = '_sku'           THEN prm.meta_value END ) as sku
     , MAX( CASE WHEN prm.meta_key = '_regular_price' THEN prm.meta_value END ) as product_price
  FROM wp_posts p
  JOIN wp_postmeta pm
    ON pm.post_id = p.ID
  JOIN wp_woocommerce_order_items i
    ON i.order_id = p.ID
  JOIN wp_woocommerce_order_itemmeta im
    ON im.order_item_id = i.order_item_id
  JOIN wp_postmeta as prm
    ON prm.post_id = im.meta_value
 WHERE p.post_type = 'shop_order'
   AND p.post_status = 'wc-processing'
   AND p.post_date >= '2016-01-14'
   AND p.post_date <  '2016-01-15'
 GROUP BY p.ID  

我们怀疑您希望将该条件包含为连接谓词。

缺少样本行,我们只是在猜测。我对你想要实现的目标的猜测是这样的结果:

prm

这并没有考虑到&#34;缺失&#34;来自i.order_item_id的行,您可能希望使用外连接操作。

在SELECT列表中使用im,并且CASE表达式从i.order_item_id返回值,看起来您可能希望在GROUP BY子句中包含{{1}}。

但同样,这些只是猜测。

答案 1 :(得分:0)

好吧 - 我最终删除了CASE语句并转向明确的JOIN语法。

select 
    p.ID as order_id,
    p.post_date,
    i.order_item_id,
    im.meta_value as product_id,
    imq.meta_value as qty,
    prm.meta_value as sku,
    prp.meta_value as product_price
from 
    wp_posts as p
join  
    wp_postmeta as pm
on
    p.ID = pm.post_id
join
    wp_woocommerce_order_items as i
on 
    p.ID = i.order_id
join
    wp_woocommerce_order_itemmeta as im
on
    i.order_item_id = im.order_item_id
join
    wp_woocommerce_order_itemmeta as imq
on
    i.order_item_id = imq.order_item_id
join  
    wp_postmeta as prm
on
    im.meta_value = prm.post_id
join  
    wp_postmeta as prp
on
    im.meta_value = prp.post_id
where 
    p.post_type = 'shop_order'
    and p.post_date BETWEEN '2016-01-14 00:00:00' AND '2016-01-14 23:59:59'
    and p.post_status = 'wc-processing'
    and im.meta_key = '_product_id'
    and imq.meta_key = '_qty'
    and prm.meta_key = '_sku'
    and prp.meta_key = '_regular_price'
group by
    p.ID  

感谢spencer7593的良好指导。