如何在yii2中编写mysql连接查询

时间:2018-01-24 12:53:56

标签: mysql yii2

var xhr=new new XMLHttpRequest();
xhr.open(url, 'get');
xhr.onreadystatechange=function () {
    // the readystate is changing,when the readystate===4
    // the request is done,then ,we can get the data
    //do something
};
xhr.send();

如何在yii2模型中编写上述查询。我想以Joins

的形式写出来

1 个答案:

答案 0 :(得分:0)

有几种方法可以使用查询构建器,Active Record,DAO为Yii2编写此查询。

但是,由于您所添加的信息或努力最少,因此甚至不提供模型的名称。我会用查询构建器\yii\db\Query();编写查询,这些是简单的joins,如果你在Yii工作,你应该知道。

$query=new \yii\db\Query();
$query
    ->select([new \yii\db\Expression('s.[[student_name]],spr.[[id]],spr.[[total_marks]],spr.[[obtained_marks]],p.[[paper_title]],p.[[exam_id]],p.[[subject_id]],sr.[[reg_no]],sr.[[student_id]],s.[[id]] as student_id')])
    ->from('students s')
    ->leftJoin('student_registration sr','sr.student_id=s.id')
    ->leftJoin('student_paper_results spr','spr.student_id=s.id')
    ->leftJoin('paper p','p.id=spr.paper_id')
    ->all();

如果您的students表型号名称为Students,请将上述代码的前2行替换为Students::find()