gpt4 book ai didi

php - Yii 多重内部连接

转载 作者:行者123 更新时间:2023-11-29 04:01:48 24 4
gpt4 key购买 nike

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

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

这将返回数组中的作业模型。我需要在 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所有列都是大写

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

最佳答案

正如@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();

关于php - Yii 多重内部连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14072672/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com