1

我正在尝试在 yii 中编写查询。我有以下工作

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

这将返回数组中的 Jobs 模型。我需要在 yii 中编写以下查询以返回模型

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 
4

2 回答 2

6

正如@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();
于 2013-01-01T11:10:01.223 回答
1

我认为您应该从中删除项目、订单->from('jobs, projects, orders')并且可能您应该降低大小写,jobs.PROJID因为您的错误消息说它找不到该列。

于 2012-12-28T17:31:00.663 回答