我收到此错误
您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在 ' ) ) AS name FROM (
resource_details) JOIN
locationON
附近使用的正确语法reso_dtail_location`'位于第 3 行
代码如下
$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();
最佳答案
按如下方式更改您的查询
$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 语句中添加 false 和 where 条件错误,并且还使用了别名,其中单个查询中有两个多个联接。
注意:根据我的理解,我已添加别名,您可以根据您的数据库结构更改它
用于在 from 子句中添加查询
$this->db->_protect_identifiers=false;
$this->db->select('ft.id,GROUP_CONCAT(ft.Name separator ",") as ColumnName',false);
$this->db->from('(select id, concat(Name, ":", GROUP_CONCAT(Value separator ",")) as Name from mytbl group by id, Name) ft');
$this->db->group_by('ft.id');
请记住,如果您想在查询上方添加联接,而不是使用 **ft* 作为别名
关于php - Mysql group_concat 查询出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38116598/