1

Zend_Db_Selectオブジェクトを使用してこのクエリを作成するにはどうすればよいですか?

SELECT
    `v1`.`id`,
    `v1`.`title`,
    `v1`.`duration`,
    `v1`.`img`
FROM 
    `videos` AS `v1` 
WHERE 
    v1.id IN (
        SELECT id_video FROM videos_categories WHERE id_video NOT IN(
            select id_video from videos_categories where id_category=34
            UNION
            select id_video from videos_categories where id_category=20
        )
    )

このようなことを試しましたが、何も機能しません。エラーページが表示されます。データマッパーを使用しています

$objQuery = $this->getDbTable()->select()
    ->from(array('v'=>'videos'),array('v.id','v.title','v.duration','v.img'))

$tableVC1 = new Application_Model_DbTable_VideosCategories(); 
$tableVC2 = new Application_Model_DbTable_VideosCategories();
$tableVC3 = new Application_Model_DbTable_VideosCategories();
$tableVC4 = new Application_Model_DbTable_VideosCategories();

// select id_video from videos_categories where id_category=20
$tableVC4->select()->from(array("vc"=>"videos_categories"),array("id_video"))
    ->where("vc.id_category=20");

// select id_video from videos_categories where id_category=34
$tableVC3->select()->from("videos_categories","id_video")
    ->where("id_category=34");

// union between previous queries
$tableVC2->select()->union(array($tableVC4,$tableVC3));

$tableVC1->select()->from("videos_categories","id_video")
    ->where("id_video NOT IN ?",$tableVC2);

$objQuery->where("v.id IN ?",$tableVC1);

私を助けてくれてありがとう。

4

3 に答える 3

1

私の推測では、文字列が必要なときにオブジェクトをユニオンに送信しようとしていると思います。

これを試して:

$objQuery = $this->getDbTable()
                 ->select()
                 ->from(array('v' => 'videos'),
                        array('v.id', 'v.title', 'v.duration', 'v.img'))

$tableVC1 = new Application_Model_DbTable_VideosCategories(); 
$tableVC2 = new Application_Model_DbTable_VideosCategories();
$tableVC3 = new Application_Model_DbTable_VideosCategories();
$tableVC4 = new Application_Model_DbTable_VideosCategories();

// select id_video from videos_categories where id_category=20
$select4 = $tableVC4->select()
                    ->from(array("vc" => "videos_categories"),
                           array("id_video"))
                    ->where("vc.id_category=20");

// select id_video from videos_categories where id_category=34
$select3 = $tableVC3->select()
                    ->from("videos_categories", "id_video")
                    ->where("id_category=34");

// union between previous queries
$select2 = $tableVC2->select()
                    ->union(array($select4, $select3));

$select1 = $tableVC1->select()
                    ->from("videos_categories", "id_video")
                    ->where("id_video NOT IN ?", $select2);

$objQuery->where("v.id IN ?", $select1);

echo $objQuery;予想されるクエリを出力する必要があります。

于 2012-04-13T15:23:48.953 に答える
0

?の周りに角かっこを作成する必要があります。=>(?)...そして変数はselectオブジェクトでなければなりません。

たとえば、次の行にあります。

$tableVC2 = $xyz->select()...

$tableVC1->select()->from("videos_categories","id_video")
        ->where("id_video NOT IN (?)",$tableVC2);
于 2012-04-13T14:55:15.640 に答える
0

モデルなしで、次のように SELECT ステートメントを作成してみてください。次のように動作するはずです。

$db = Zend_Db_Table::getDefaultAdapter();
$select = $db->select()
    ->from(
        // base table
    )
    ->joinLeft(
        // join sth
    )
    ->where('x = ?', $x)
    ->where('y = ?', $y)
    ->order('z DESC')
    ->limit(10, 0);
$q = $db->query($select);
$rowSet = $q->fetchAll();
于 2012-04-13T15:04:30.980 に答える