我正在尝试准备以下 sql 查询,但不幸的是它不起作用。
select course.course_name, enroll.start_date from dp_courses course LEFT OUTER JOIN dp_enroll enroll ON course.id = enroll.course_id and enroll.student_id=3
$criteria=new CDbCriteria(array(
'order'=>'update_time DESC',
'with'=> array('enroll'),
));
$criteria->alias = 'course';
$criteria->together=true;
$criteria->join='LEFT OUTER JOIN dp_enroll ON dp_enroll.course_id=course.id and dp_enroll.student_id='. Yii::app()->user->id;
//$criteria->condition='dp_enroll.student_id='. Yii::app()->user->id;
课程模型中的关系是:
public function relations() {
return array(
'enroll'=> array(self::HAS_MANY, 'Enroll', 'course_id'),
'author' => array(self::BELONGS_TO, 'YumProfile', 'author_id'), ); }
但生成的 sql 查询我想要什么
LEFT OUTER JOIN dp_enroll enroll ON course.id = enroll.course_id **and** enroll.student_id=3
使用and运算符在两个属性上连接表