隐藏我正在计算的列

时间:2018-07-25 15:20:26

标签: mysql sql mysqli

在我的查询中,我找到了payment_method,删除了所有不具有“ cod”值的行,但是我想从输出中删除此列。我该怎么办?

SELECT
    max( CASE WHEN um.meta_key = 'hrid' THEN um.meta_value END ) AS HRID,
    max( CASE WHEN pm.meta_key = '_order_total' AND p.ID = pm.post_id THEN pm.meta_value END ) AS order_total,
    max( CASE WHEN pm.meta_key = '_payment_method' AND p.ID = pm.post_id THEN pm.meta_value END ) AS payment_method
FROM wp_posts p
    LEFT JOIN wp_postmeta pm ON p.ID = pm.post_id
    LEFT JOIN wp_woocommerce_order_items oi ON p.ID = oi.order_id
    LEFT JOIN wp_users u ON u.ID = pm.meta_value AND pm.meta_key = '_customer_user'
    LEFT JOIN wp_usermeta um ON um.user_id = u.ID
WHERE post_type = 'shop_order' and post_status = 'wc-completed'    
GROUP BY p.ID
HAVING payment_method = 'cod'


Result right now
HRID    order_total    payment_method
x       y              z

Expected result
HRID    order_total
x       y          

1 个答案:

答案 0 :(得分:0)

将条件移至having子句:

SELECT max( CASE WHEN um.meta_key = 'hrid' THEN um.meta_value END ) AS HRID,
       max( CASE WHEN pm.meta_key = '_order_total' AND p.ID = pm.post_id THEN pm.meta_value END ) AS order_total
FROM wp_posts p
    LEFT JOIN wp_postmeta pm ON p.ID = pm.post_id
    LEFT JOIN wp_woocommerce_order_items oi ON p.ID = oi.order_id
    LEFT JOIN wp_users u ON u.ID = pm.meta_value AND pm.meta_key = '_customer_user'
    LEFT JOIN wp_usermeta um ON um.user_id = u.ID
WHERE post_type = 'shop_order' and post_status = 'wc-completed'    
GROUP BY p.ID
HAVING  max( CASE WHEN pm.meta_key = '_payment_method' AND p.ID = pm.post_id THEN pm.meta_value END ) = 'cod'