2016-06-30 111 views
0

我收到此错误 您的SQL语法错误;检查对应于你的MySQL服务器版本使用附近的“)) AS name FROM ( resource_details ) JOIN位置ON reso_dtail_location`”在行3Mysql group_concat查询给出错误

代码如下

$this->db->select('loc_country,CONCAT(s_name , ":", GROUP_CONCAT(DISTINCT dist_name 
    ORDER BY dist_name ASC 
    SEPARATOR ",")) AS name '); 


    $this->db->from('resource_details'); 
      //join 
      $this->db->join('location','reso_dtail_location=loc_id');   
      $this->db->join('go_state', 'go_stste_id = loc_state', 'left'); 
      $this->db->join('go_country', 'num = loc_country', 'left'); 
      $this->db->join('go_dist', 'id = loc_district', 'left'); 
      $this->db->where('loc_id !=1 AND loc_id !=2'); 
      $this->db->group_by('country_name'); 
      $query = $this->db->get(); 
+0

不确定CodeIgniter创建的完整语句是什么,但是您可能希望将's_name'添加到您的“GROUP BY”列表中 –

+1

对于复杂的操作,建议使用'$ this-> db-> query('YOUR PLAIN QUERY HERE');'[Docs](https://codeigniter.com/userguide3/database/results.html)。 – Tpojka

回答

1

更改您的查询像下面

正确的语法手册
$this->db->select('loc_country,CONCAT(s_name , ":", GROUP_CONCAT(DISTINCT dist_name ORDER BY dist_name ASC SEPARATOR ",")) AS name',false); 
$this->db->from('resource_details rd'); 
      //join 
      $this->db->join('location l','rd.reso_dtail_location=l.loc_id');   
      $this->db->join('go_state gs', 'gs.go_stste_id = l.loc_state', 'left'); 
      $this->db->join('go_country gc', 'gc.num = l.loc_country', 'left'); 
      $this->db->join('go_dist gd', 'gd.id = l.loc_district', 'left'); 
      $this->db->where('l.loc_id !=',1); 
$this->db->where('l.loc_id !=',2); 
      $this->db->group_by('gc.country_name'); 
      $query = $this->db->get(); 

二是从现有查询变化添加虚假的select语句其中条件有误并且还使用了在单个查询中有两个许多联接的别名。

注:我添加别名按我的理解,你可以改变它根据你的数据库结构

对于FROM子句

​​

记得添加查询,如果你想添加连接上面的查询,而不是使用** ft *作为别名

+0

select id,group_concat('Name' separator',')as'ColumnName' from ( select id,concat('Name',':', group_concat('Value' separator','))as' Name' from mytbl group by id,'Name' )tbl group by id;我想在codeigniter这个查询如何我可以转换它plz帮助 – riya

+0

所以你想在你的声明中添加另一个查询。你想要这个吗? –

+0

是的,我想添加一个查询 – riya