我有以下MySQL来汇总来自几个表的数据:
SELECT * FROM $wpdb->posts
LEFT JOIN taxi ON ($wpdb->posts.ID = taxi.taxiID)
LEFT JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
WHERE $wpdb->posts.post_status = 'publish'
AND $wpdb->posts.post_type = 'post'
AND $wpdb->posts.post_date < NOW()+INTERVAL 1 DAY
AND $wpdb->term_taxonomy.taxonomy = 'category'
AND $wpdb->term_taxonomy.term_id IN(3)
AND taxi.taxiID IS NULL
AND ($wpdb->postmeta.meta_key = 'EndDate' AND $wpdb->postmeta.meta_value > DATE_ADD(NOW(), INTERVAL 3 HOUR))
截至昨天,查询没有返回任何结果,但这里是踢球者:它过去常常连续两周返回结果!
经过大量的反复试验,我把问题缩小到这一行:
AND taxi.taxiID IS NULL
该行应该导致结果过滤以仅显示那些在出租车表中没有记录的帖子。
任何人都可以帮我弄清楚如何解决问题吗?
答案 0 :(得分:0)
如果此查询位于heredoc中,请明确插值。 http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
$query = <<<HERE
SELECT * FROM {$wpdb->posts}
LEFT JOIN userContests ON ({$wpdb->posts}.ID = taxi.taxiID)
LEFT JOIN {$wpdb->term_relationships} ON ({$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id)
LEFT JOIN {$wpdb->term_taxonomy} ON ({$wpdb->term_relationships}.term_taxonomy_id = {$wpdb->term_taxonomy}.term_taxonomy_id)
WHERE {$wpdb->posts}.post_status = 'publish'
AND {$wpdb->posts}.post_type = 'post'
AND {$wpdb->posts}.post_date < NOW()+INTERVAL 1 DAY
AND {$wpdb->term_taxonomy}.taxonomy = 'category'
AND {$wpdb->term_taxonomy}.term_id IN(3)
AND ({$wpdb->postmeta}.meta_key = 'EndDate' AND {$wpdb->postmeta}.meta_value > DATE_ADD(NOW(), INTERVAL 3 HOUR))
HERE
答案 1 :(得分:0)
您的PHP脚本超时是否正在发生? 请记住,只是b / c SQL查询没有改变,并不意味着数据也没有。如果查询过于复杂或数据太多,则可能会导致超时。这可以解释为什么它在phpmyadmin中工作,而不是你的PHP脚本。 phpmyadmin添加了一个LIMIT子句,允许mysql服务器只返回一小部分结果,因此很少超时。
尝试将max_execution_time设置为更高的值。
ini_set('max_execution_time', 300); //300 seconds = 5 minutes