yii multiple inner joins

2019-07-24 17:31发布

I am trying to write a query in yii. I have the following which works

$criteria = new CDbCriteria;
$criteria->condition = "($column = :id)";
$criteria->params = array(":id" =>  $id );
$rows = Jobs::model()->with('pROJ')->findAll($criteria);

This returns the model of Jobs in array. I need to write the following query in yii to return a model

SELECT jobs.JOBNO, jobs.STATUS, projects.ORDERNO, jobs.PROJID, jobs.NAME, jobs.SEQ, jobs.PCENTDONE, jobs.EARNED, jobs.VALUE, jobs.DATEIN, jobs.DATEDONE, jobs.DATEDUE, jobs.SENTBACK, jobs.ORIGTAPES, jobs.COMMENTS, projects.CATEGORY, orders.BIDNO
FROM (jobs INNER JOIN projects ON jobs.PROJID = projects.PROJID) INNER JOIN orders ON projects.ORDERNO = orders.ORDERNO
where jobs.projid =     3002001
ORDER BY jobs.JOBNO, jobs.PROJID 

I have tried the following but it does not work

$rows = Yii::app()->db->createCommand()
            ->select('jobs.*, projects.ORDERNO, projects.CATEGORY, orders.BIDNO')
            ->from('jobs, projects, orders')
            ->join('projects p','jobs.PROJID = p.PROJID')
            ->join('orders o', 'p.ORDERNO = o.ORDERNO')
            ->where('jobs.projid=:id', array(':id'=>$id))
            ->queryRow(); 

I get the following error

CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'jobs.PROJID' in 'on clause'. The SQL statement executed was: SELECT `jobs`.*, `projects`.`ORDERNO`, `projects`.`CATEGORY`, `orders`.`BIDNO`
FROM `jobs`, `projects`, `orders`
JOIN `projects` `p` ON jobs.PROJID=p.PROJID
JOIN `orders` `o` ON p.ORDERNO=o.ORDERNO
WHERE jobs.projid=:id 

I have updated to

$rows = Yii::app()->db->createCommand()
                ->select('jobs.*, projects.orderno, projects.category, orders.bidno')
                ->from('jobs')
                ->join('projects p','jobs.projid = p.projid')
                ->join('orders o', 'p.orderno = o.orderno')
                ->where('jobs.projid=:id', array(':id'=>$id))
                ->queryRow();  

but i still get the error. All columns in mysql are CAPS

CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'projects.orderno' in 'field list'. The SQL statement executed was: SELECT `jobs`.*, `projects`.`orderno`, `projects`.`category`, `orders`.`bidno`
FROM `jobs`
JOIN `projects` `p` ON jobs.projid = p.projid
JOIN `orders` `o` ON p.orderno = o.orderno
WHERE jobs.projid=:id 

标签: php mysql yii
2条回答
Fickle 薄情
2楼-- · 2019-07-24 18:08

I think you should remove projects, orders from ->from('jobs, projects, orders') and may be you should lower the case of jobs.PROJID as your error message says that it couldn't find the column.

查看更多
来,给爷笑一个
3楼-- · 2019-07-24 18:21

As @DCoder said: your select should now read select('jobs.*, p.orderno, p.category, o.bidno'). For consistency you should also alias jobs as follows

$rows = Yii::app()->db->createCommand()
            ->select('j.*, p.orderno, p.category, o.bidno')
            ->from('jobs j')
            ->join('projects p','j.projid = p.projid')
            ->join('orders o', 'p.orderno = o.orderno')
            ->where('j.projid=:id', array(':id'=>$id))
            ->order('j.jobno,j.projid')
            ->queryRow();
查看更多
登录 后发表回答