使用 CodeIgniter 分组

Toj*_*ojo 6 php mysql join group-by codeigniter

我在我的项目中使用 CodeIgniter,这是我在模型中的代码

public function group_all_ville(){  
        $this->db->select('*');
        $this->db->from('departement');
        $this->db->join('villes', 'villes.num_dept = departement.num_dept');
        $this->db->group_by('nom_dept'); 
        $query = $this->db->get(); 
        return $query->result();
    }
Run Code Online (Sandbox Code Playgroud)

这是执行后的错误

A Database Error Occurred

Error Number: 1055

Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'pneu.departement.id_dept' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

SELECT * FROM `departement` JOIN `villes` ON `villes`.`num_dept` = `departement`.`num_dept` GROUP BY `nom_dept`

Filename: C:/wamp64/www/pneu/system/database/DB_driver.php

Line Number: 691
Run Code Online (Sandbox Code Playgroud)

我尝试了不同的方法,但没有任何结果。谢谢

Abd*_*lam 7

也使用order_by子句

$this->db->group_by('nom_dept'); 
$this->db->order_by('nom_dept', 'asc');  # or desc
Run Code Online (Sandbox Code Playgroud)

仅供参考:设置 SQL 模式和会话集并不能修复实际错误。

例子(最好不要做

  1. /sf/answers/2501077701/