WordPress子查询在SELECT

时间:2016-04-14 17:32:25

标签: php mysql sql wordpress subquery

我正在通过自定义Wordpress插件使用核心cron执行此查询:

// MAKE SQL CALL
    $SQL = "SELECT ".$wpdb->prefix."postmeta.post_id FROM ".$wpdb->prefix."postmeta
    INNER JOIN ".$wpdb->prefix."posts ON (".$wpdb->prefix."posts.ID = ".$wpdb->prefix."postmeta.post_id )       
     WHERE ".$wpdb->prefix."postmeta.meta_key = '".$core_admin_values['listing_expiration']['key']."' 
     AND ".$wpdb->prefix."posts.post_status = 'publish'
     AND ".$wpdb->prefix."postmeta.post_id = (SELECT ".$wpdb->prefix."postmeta.post_id FROM ".$wpdb->prefix."postmeta WHERE ".$wpdb->prefix."postmeta.meta_key = 'listing_status' AND ".$wpdb->prefix."postmeta.meta_value != 1)
     AND ".$wpdb->prefix."posts.post_type = '".$core_admin_values['listing_expiration']['taxonomy']."_type'
     AND DATE(".$wpdb->prefix."postmeta.meta_value) < DATE(NOW())";     
    $expired_listings = (array)$wpdb->get_results($SQL);

但是返回此错误:

  

在stderr中发送的FastCGI:&#34; PHP消息:WordPress Subquery的数据库错误为SELECT返回多行

如何解决这个问题?我在Stack中尝试了类似问题的一些解决方案,但它仍然失败。

2 个答案:

答案 0 :(得分:0)

尝试在子查询

上添加input

// MAKE SQL CALL

100px

答案 1 :(得分:0)

为了易读起见,我重新格式化了您的查询。

SELECT 
  postmeta.post_id 
FROM 
  postmeta
  INNER JOIN posts 
    ON (posts.ID = postmeta.post_id )       
WHERE 
  postmeta.meta_key = '".$core_admin_values['listing_expiration']['key']."' 
  AND posts.post_status = 'publish'
  AND postmeta.post_id = 
  (
      SELECT 
        postmeta.post_id 
      FROM 
        postmeta 
      WHERE 
        postmeta.meta_key = 'listing_status' 
        AND postmeta.meta_value != 1
  )
  AND posts.post_type = '".$core_admin_values['listing_expiration']['taxonomy']."_type'
  AND DATE(postmeta.meta_value) < DATE(NOW())";     

我怀疑你的问题源于你的where子句中的子查询。

      SELECT 
        postmeta.post_id 
      FROM 
        postmeta 
      WHERE 
        postmeta.meta_key = 'listing_status' 
        AND postmeta.meta_value != 1

并且这个块返回了多个结果。如果要匹配多个结果,请将“=”更改为“in”,如下所示:

  AND postmeta.post_id in
  (
      SELECT 
        postmeta.post_id ...
  )