何か作業をする必要があります:
$subselect = $dbAdapther->select()->from(
array('test_stats' => 'test_stats'),
array(
'(COUNT(test_public_id)) AS recent_views'
)
)->where(
$dbAdapther->quoteInto('test_stats.test_public_id = ?', $tourId)
)->where(
'test_stats.updated_on > DATE_SUB(CURDATE(), INTERVAL 7 DAY)'
);
$select = $dbAdapther->select()->from(
array('ms' => 'test_stats'),
array(
'(COUNT(ms.test_public_id)) AS total_views' // COUNT should be in brackets to preevent Zend from interpreting it as a field name
)
)->join(
array('ms1' => $subselect),
'',
array(
'ms1.recent_views'
)
)->where(
$dbAdapther->quoteInto('ms.test_public_id = ?', $tourId)'
);
私が持っていると思いますが、あなたのクエリは2つの別々のものに分かれていますし、より正確には日付をパラメータとする普遍的な「ビュー数を取得」クエリを記述し、それを日付の有無にかかわらず2回呼びます。
しかし、まだ2つの数字を1つの行に入れる必要がある場合(つまり、不要なJOINの代わりにUNIONを使用することはできません)、代わりに次のコードを使用することをおすすめします。
$select = $dbAdapther->select()->from(
array('ms' => 'test_stats'),
array(
'(COUNT(ms.test_public_id)) AS total_views',
'(
COUNT(
CASE
WHEN ms.updated_on > DATE_SUB(CURDATE(), INTERVAL 7 DAY)) THEN ms.test_public_id
ELSE NULL
END
)
) AS recent_views'
)
)->where(
$dbAdapther->quoteInto('ms.test_public_id = ?', $tourId)
);
エラーメッセージや予期しない結果など、正確に「機能していない」ものを記述すると便利です。たとえば、あなたが示したコードには引用符がありません( '$ query ='の後と 'select'の前)タイプミスに過ぎない。より多くの情報が良いでしょう。 – StasM