我想准备一个 SQL 查询,我想在其中放入 UNION 查询,但是 UNION 查询是基于特定条件的,是否可以使用 Yii 查询构建器进行。
下面是我用简单的 DAO 制作的示例代码,但我想以查询生成器风格制作它
if(isset($first) && $first!="")
$con .= "SELECT id,first, from user where first LIKE '%".$first."%'";
if(isset($last) && $last!="")
$con .= " AND last LIKE '%".$last."%'";
if((!empty($first) || !empty($last)) && (!empty($flag)) )
$con .= " UNION SELECT id,first, from user where flag = 1"
$command = $connection->createCommand($con)->queryall();
最佳答案
是的,确实如此。假设这是您的测试代码:
$command = Yii::app()->db->createCommand();
if(<your if statements here) {
$command->select('id, username, profile')
}
if(<union if statement>) {
$command->union()
}
一些来自 Query Builder page 的其他行样本
->from('tbl_user u')
->join('tbl_profile p', 'u.id=p.user_id')
->where('id=:id', array(':id'=>$id))
->queryRow();
但是您要查看的主要内容是 union()
关于php - SQL 条件 UNION() 与 Yii 查询生成器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13474345/