我在这里阅读了几个问题和答案,但我似乎无法找到适合我的问题的解决方案......
我会尽量说清楚:
首先,我在 phpmyadmin 中测试了我的 SQL 查询,它按我想要的方式工作:
SELECT s. *
FROM PERSONNE_UDS p, ETUDIANT e, ENTREPRISE ent, STAGE s
LEFT JOIN PERSONNE_UDS pt ON s.tuteur = pt.id_ens
WHERE p.id_etudiant = s.id_etudiant
AND s.id_etudiant = e.id_etudiant
AND ent.id_entreprise = s.id_entreprise
AND p.code_dept =4
AND s.code_per_cal = '2016s2'
AND s.id_formation = 'SC40'
AND s.id_up =2
我补充说:
ORDER BY pt.nom ;
在我的模型类中,我将它转换成这样:
$q = $this
->createQuery()
->select('s.*')
->from('Stage s')
->addFrom('PersonneUds p')
->addFrom('Etudiant e')
->addFrom('Entreprise ent')
->leftJoin('PersonneUds pt ON s.tuteur = pt.id_ens')
->where('p.id_etudiant = s.id_etudiant')
->addWhere('s.id_etudiant = e.id_etudiant')
->addWhere('ent.id_entreprise = s.id_entreprise');
if(is_array($dept)){
$q->andWhereIn('p.code_dept', $dept);
} else if($dept != -1){
$q->andWhere('p.code_dept = ?', $dept);
}
$q->addWhere('s.code_per_cal = ?', $code_per_cal)
->addWhere('s.id_formation = ?', $id_formation)
->addWhere('s.id_up = ?', $id_up);
switch ($order_columns){
case 'nom':
$q->orderBy('p.nom '.$order_type);
break;
case 'entreprise':
$q->orderBy('ent.nom '.$order_type);
break;
case 'sujet':
$q->orderBy('s.intitule_stage '.$order_type);
break;
case 'tuteur':
//$q->orderBy('s.tuteur '.$order_type);
$q->orderBy('pt.nom '.$order_type);
//$q->orderBy('pt.nom '.$order_type);
break;
default:
$q->orderBy('s.etat_stage '.$order_type);
$q->orderBy('p.nom DESC');
break;
}
$q->execute();
当我尝试我的应用程序时,它似乎设法进行了连接,但没有按...进行排序
该应用程序是用 PHP5 使用 CodeIgniter 和 Doctrine 编写的。
有什么想法吗?
抱歉这个问题太长了。
2017 年 5 月 23 日编辑:除了
case 'tuteur':
//$q->orderBy('s.tuteur '.$order_type);
$q->orderBy('pt.nom '.$order_type);
//$q->orderBy('pt.nom '.$order_type);
break;
最佳答案
Codeigniter 事件记录应如下所示:
$q->order_by('column', 'ASC');
关于php - LEFT JOIN 后的 ORDER BY 不能在浏览器中工作但在 SQL 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44111446/