2011-11-15 9 views
-1

私は別のクエリで自分自身を試しましたが、これは私が新しいzendになるために私にとってはもっと複雑です。私はさまざまな方法を試してみましたが、うまくいきませんでした。zend dbでSQLクエリを実装する方法

Tour Id fetching from another query 

$tourId = $row2 ['test_public_id']; 

$query = 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'" ; 
+0

エラーメッセージや予期しない結果など、正確に「機能していない」ものを記述すると便利です。たとえば、あなたが示したコードには引用符がありません( '$ query ='の後と 'select'の前)タイプミスに過ぎない。より多くの情報が良いでしょう。 – StasM

答えて

1

何か作業をする必要があります:

$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) 
); 
0

私もこのサンプルを試してみましたが、その作品です。 このチュートリアルを参照してください、私はそれはあなたを助けるいただければ幸いです: http://framework.zend.com/manual/en/zend.db.select.html

か、この操作を行うことができます。そのような

$db = Zend_Db_Table_Abstract::getDefaultAdapter(); 
$stmt = $db->query($query); 
$result = $stmt->fetchAll(); 
関連する問題