[英]Zend_Db_Select query with UNION and IN operator
How can i write this query with Zend_Db_Select objects? 如何使用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
)
)
I tried something like this but nothing works, i've got an error page. 我尝试了类似的方法,但是没有任何效果,我的页面出现了错误。 I use 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 for helping me. 感谢您的帮助。
My guess is that you're trying to send an object to your union, when it expects a string. 我的猜测是,当您期望一个字符串时,您正在尝试将一个对象发送到您的联合。
Try this: 尝试这个:
$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;
should output the expected query. 应该输出预期的查询。
You have to make brackets around the ?. 您必须在?前面加上方括号。 => (?) ... and the variable has to be a select object. =>(?)...,并且该变量必须是一个选择对象。
For example in this line: 例如在这一行:
$tableVC2 = $xyz->select()...
$tableVC1->select()->from("videos_categories","id_video")
->where("id_video NOT IN (?)",$tableVC2);
Try build your SELECT statements like this, without models - it should work this way: 尝试像这样在没有模型的情况下构建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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.