我想准备一个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();
答案 0 :(得分:1)
是的,确实如此。假设这是你的测试代码:
$command = Yii::app()->db->createCommand();
if(<your if statements here) {
$command->select('id, username, profile')
}
if(<union if statement>) {
$command->union()
}
的其他一些示例
->from('tbl_user u')
->join('tbl_profile p', 'u.id=p.user_id')
->where('id=:id', array(':id'=>$id))
->queryRow();
但您要查看的主要内容是union()