2016-06-30 110 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