定义的变量在yii中不能与createCommand一起使用

时间:2015-03-29 11:45:21

标签: php mysql sql yii

我使用createCommand并运行以下查询但我的项目中出现错误

$all_project_tasks = Yii::app()->db->createCommand("
            set @prevOrder=0;
            set @mm=0;
            set@dt=0;
            select * from project_task_user_id ptui right join
            (select id,`desc`,due_time,list_id,if(@prevOrder=`order`,
            @dt,@dt:=@dt+due_time) this_finish_due,if(@prevOrder=`order`,
            @mm,if(@prevOrder=0,@mm,@mm:=@dt-due_time)) prev_due,@prevOrder:=`order` as `order` 
            from (select id,`desc`,due_time,`order`,list_id from project_task a 
            where a.list_id in (select id from project_list where project_id=1 ) 
            and (a.due_time is not null) order by a.order asc,a.due_time desc ) as b) l
            ON ptui.project_task = l.id where ptui.member_id =7 ")
    ->queryAll();

并收到此错误

  

SQLSTATE [HY000]:常规错误。执行的SQL语句是

为什么会出现此错误?哪里出错了? creatCommand不支持sql中的@variable?

1 个答案:

答案 0 :(得分:5)

Yii的createCommand基于PDO,因此它不支持多个查询执行。

$all_project_tasks = Yii::app()->db->createCommand("set @prevOrder=0")->execute();
       $all_project_tasks = Yii::app()->db->createCommand("set @mm=0")->execute();
       $all_project_tasks = Yii::app()->db->createCommand("set @dt=0")->execute();
       $all_project_tasks = Yii::app()->db->createCommand('select * from project_task_user_id ptui right join (select id,`desc`,due_time,list_id,if(@prevOrder=`order`,@dt,@dt:=@dt+due_time) this_finish_due,if(@prevOrder=`order`,@mm,if(@prevOrder=0,@mm,@mm:=@dt-due_time)) prev_due,@prevOrder:=`order` as `order`
                                            from (select id,`desc`,due_time,`order`,list_id from project_task a where a.list_id in (select id from project_list where project_id=1 )and (a.due_time is not null) order by a.order asc,a.due_time desc ) as b) l
                                                ON ptui.project_task = l.id where ptui.member_id =7')->queryAll();

我认为这一定很好。