2012-04-13 60 views
1

如何使用Zend_Db_Select对象编写此查询?Zend_Db_Select使用UNION和IN运算符查询

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 
     ) 
    ) 

我试过这样的东西,但没有任何作品,我有一个错误页面。我使用datamappers

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

Thx帮助我。

+0

你有什么类型的“错误页面”? – Liyali 2012-04-13 15:19:59

+0

应用程序错误...没有显示,我试图做$ objQuery - > __ toString()但没有再次 – 2012-04-13 15:20:51

回答

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;应该输出预期的查询。

+0

Thx Liyali !!!它的作品...你能解释我为什么现在它可以工作吗?我将相同的obj传递给$ select var – 2012-04-13 15:31:02

+0

'$ select [1 | 2 | 3 | 4]'是'Zend_Db_Select'的实例,它实现了'__toString()'方法。如果你直接向你的union(或where子句)传递'$ tableVC1',这意味着你正在尝试传递一个'Application_Model_DbTable_VideosCategories'的实例。 – Liyali 2012-04-13 15:45:45

0

你必须围绕?做括号。 =>(?) ...并且变量必须是选择对象。

例如,在这一行:

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

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

我试过了,但我没有工作 – 2012-04-13 14:57:02

+0

我发布了另一个答案,也许这会帮助你。 – ryu 2012-04-13 15:04:53

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(); 
+0

我有超过100万条记录的数据库,问题是我需要UNION和IN运算符,同一查询加入需要2分钟,IN和UNION只有0.020秒 – 2012-04-13 15:09:19

+0

我并不是说你应该使用join。我的意思是你应该使用这种构建你的选择语句。这样,您可以在另一个select语句的where方法调用中使用变量$ select(完整的select对象)。如果你使用Zend模型来做这件事,那就是我的经验。 – ryu 2012-04-13 15:16:00