php - Mysql group_concat 查询出现错误

标签 php mysql sql codeigniter

我收到此错误 您的 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 语句中添加 falsewhere 条件错误,并且还使用了别名,其中单个查询中有两个多个联接。

注意:根据我的理解,我已添加别名,您可以根据您的数据库结构更改它

用于在 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/

相关文章:

mysql - 如何修复此选择查询

php - 仅在 PHP 中上传某些文件(运行 Docker for Windows)

php - 获取服务器的 SPF 记录

sql - 在Oracle中,如何在聚合所有行时选择特定行

php - 多个内部连接在 PHP 中不起作用

mysql - 为什么 mysql 在子查询的组连接上返回错误?

mysql - 选择使用不同的 mysql 进行更新

php - 检查两个SQL表

php - 在配置文件中访问 session 或 Yii

java - 多用户登录java(管理员,用户,教师)