警予多个内部连接(yii multiple inner joins)

2019-10-17 13:06发布

我试图写在警予的查询。 我有工作的下面

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

这将返回乔布斯在阵列模式。 我需要写下面的查询在警予返回模型

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 

我曾尝试以下,但它不工作

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

我得到以下错误

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 

我已经更新到

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

但我仍然得到错误。 在MySQL中的所有列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 

Answer 1:

正如@DCoder说:你的选择现在应该读select('jobs.*, p.orderno, p.category, o.bidno') 为了保持一致性,你应该也别名jobs如下

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


Answer 2:

我想你应该删除项目,订单 ->from('jobs, projects, orders')并可能是你应该降低的情况下jobs.PROJID因为您的错误消息说,它无法找到该列。



文章来源: yii multiple inner joins
标签: php mysql yii