2016-02-25 85 views
0

我有這個疑問,現在MySQL的INNER後附加條件JOIN,ON

global $wpdb; 
$interval = "1 WEEK"; 
$now = current_time('mysql'); 
$top4=$wpdb->get_results('SELECT ID, post_title, post_name from `'.$wpdb->prefix.'popularpostssummary`  
INNER JOIN `'.$wpdb->prefix.'posts` ON `postid`=`ID` 
ORDER BY `pageviews` DESC 
LIMIT 4;', ARRAY_A); 

我要添加下列條件的,什麼是在代碼中加入他們的正確方法?

WHERE post_type = post 
AND last_viewed > DATE_SUB('{$now}', INTERVAL {$interval}) 

post_type是在」。$ wpdb-> prefix.'posts while last_viewed is under '.$wpdb->prefix.'popularpostssummary

回答

1
global $wpdb; 
$interval = "1 WEEK"; 
$now = current_time('mysql'); 
$sql = 
    'SELECT p.ID, p.post_title, p.post_name 
    FROM `' . $wpdb->prefix . 'popularpostssummary` AS pps 
     INNER JOIN `' . $wpdb->prefix . 'posts` AS p ON pps.`postid`= p.`ID` 
    WHERE p.post_type = "post" 
     AND pps.last_viewed > DATE_SUB("' . $now . '", INTERVAL ' . $interval . ') 
    ORDER BY pps.`pageviews` DESC 
    LIMIT 4;'; 
echo $sql; exit; 
$top4 = $wpdb->get_results($sql, ARRAY_A); 
+0

感謝您的答覆佩斯先生,不幸的是我得到了服務器錯誤500 –

+0

我覺得單引號圍繞「{$ now}」干擾解析PHP。我修改了答案,以便輸出SQL。你有可以用來對數據庫運行查詢的SQL客戶端嗎? –

+0

我這樣做,但如何全球$ wpdb,我認爲它不會在客戶端 –