在商业活动开始前2周,按销售价格日期获取产品id,但不包括原始报价产品id

3b6akqbq  于 2021-06-17  发布在  Mysql
关注(0)|答案(1)|浏览(294)

我正在使用woocommerce和offer for woocommerce。我想得到没有报价的产品。我准备了代码,但它不起作用:(我在第二个查询中尝试了:select distinct(wp_postmeta.meta_value)…,但它也不起作用。

$draftid = $wpdb->get_col('SELECT wp_posts.ID FROM ' . $wpdb->postmeta . ' join ' . $wpdb->posts . ' ON wp_posts.ID = wp_postmeta.post_id 
WHERE wp_posts.post_status="draft" and wp_postmeta.meta_key="_sale_price_dates_to" and wp_postmeta.meta_value != "" and wp_postmeta.meta_value<="' . (time()-1296000) . '"  
EXCEPT SELECT wp_postmeta.meta_value FROM ' . $wpdb->postmeta . ' join ' . $wpdb->posts . ' ON wp_posts.ID = wp_postmeta.post_id
WHERE wp_postmeta.meta_key="orig_offer_product_id" and wp_postmeta.meta_value !="" and wp_posts.post_type="woocommerce_offer"'); print_r($draftid);

但第一个查询和第二个查询分别起作用。如果我用“union”而不是“except”,那也行。
知道为什么吗?

2hh7jdfx

2hh7jdfx1#

在没有任何保证的情况下尝试以下操作:

global $wpdb;

$draft_id = $wpdb->get_col("
    SELECT p.ID 
    FROM {$wpdb->prefix}posts as p
    JOIN {$wpdb->prefix}postmeta as pm ON p.ID = pm.post_id 
    WHERE p.post_status = 'draft' 
    AND pm.meta_key = '_sale_price_dates_to' 
    AND pm.meta_value ! = '' 
    AND pm.meta_value <= " . (time()-1296000) . "
    AND p.ID NOT IN (
        SELECT p2.ID 
        FROM {$wpdb->prefix}posts as p2
        JOIN {$wpdb->prefix}postmeta as pm2  ON p2.ID = pm2.post_id 
        AND pm2.meta_key = 'orig_offer_product_id' 
        AND pm2.meta_value ! = ''
        AND p2.post_type = 'woocommerce_offer'
    )
"); 

print_r($draft_id);

我希望它能奏效。

相关问题