2011-11-14 95 views
0

這是我的查詢我在哪裏每週獲取和總看法Zend的查詢臨時表

select count(ms.test_public_id) as total_views, ms1.recent_views from test_stats ms 
join (select count(test_stats.test_public_id) as recent_views from test_stats 
where test_stats.test_public_id = '$tourid' 
and test_stats.updated_on > DATE_SUB(CURDATE(), INTERVAL 7 DAY)) ms1 
where ms.test_public_id ='$tourid' 

我試圖在Zend中實現輸出是這樣

$cols = array ('count(ms.test_public_id) as total_views', 'ms1.recent_views', 'count(test_stats.test_public_id) as recent_views'); 
$select = $db ->select() 
->from ('test', $cols) 
->join ('user', 'user.user_id = test.user_id', array()) 
->join ('test_stats', 'test_stats.test_public_id =test.test_public_id', array()) 
->where ('test_stats.test_public_id = ?', '$testId') 
->where ('test_stats.updated_on > DATE_SUB(CURDATE(), INTERVAL 7 DAY)') 
->where ('ms.test_public_id = ?', '$testId') 
->group ('test_stats.test_public_id') 
->order ('title'); 
$result = $db->fetchAll ($select); 

請糾正我在哪裏我做錯了..........

回答

0

->join ('user', 'user.user_id = test.user_id', array());爲什麼是空數組?和標題訂單我覺得....試試echo $ select = ....看看執行什麼查詢和比較你想要的。

TRY

$cols = array ('count(ms.test_public_id) as total_views', 'ms1.recent_views', 'count(test_stats.test_public_id) as recent_views'); 
    echo $select = $db ->select() 
    ->from ('test', $cols) 
    ->join ('user', 'user.user_id = test.user_id', array()) 
    ->join ('test_stats', 'test_stats.test_public_id =test.test_public_id', array()) 
    ->where ('test_stats.test_public_id = ?', '$testId') 
    ->where ('test_stats.updated_on > DATE_SUB(CURDATE(), INTERVAL 7 DAY)') 
    ->where ('ms.test_public_id = ?', '$testId') 
    ->group ('test_stats.test_public_id') 
    ->order ('test.title'); 
    $result = $db->fetchAll ($select); 
+0

可以請您建議的更改 – user1028072

+0

糾正它,當我想這我得到錯誤未定義ms.test_public_id – user1028072

+0

變化 - >在哪裏( '?ms.test_public_id =', '$ testId') 'ms'給你'是正確的表名...我只是猜測 - > where('test_stats.test_public_id =?','$ testId') –