Reputation: 588
i wanted to prepare one SQL query in which i wanted to put UNION query but that UNION query is based on certain condition, is it possible to make using Yii query builder.
below is sample code which i have made with simply DAO but i want to make it in query builder style
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();
Upvotes: 0
Views: 1851
Reputation: 5955
Yes, it sure is. Assuming this were your test code:
$command = Yii::app()->db->createCommand();
if(<your if statements here) {
$command->select('id, username, profile')
}
if(<union if statement>) {
$command->union()
}
Some sample other lines from the Query Builder page
->from('tbl_user u')
->join('tbl_profile p', 'u.id=p.user_id')
->where('id=:id', array(':id'=>$id))
->queryRow();
But the main one you'll want to look at is union()
Upvotes: 1