0

UNIONクエリを入れたいSQLクエリを1つ作成したかったのですが、その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();
4

1 に答える 1

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

于 2012-11-20T21:35:23.263 に答える