【问题标题】:How to write query in codeigniter for group_concat and concat with replace for table column如何在codeigniter中为group_concat和concat编写查询并替换表列
【发布时间】:2019-04-03 13:28:48
【问题描述】:

我得到用逗号分隔的列值并在单引号内,这在 phpmyadmin 中工作正常并获得输出,但我想使用 $this->db->query() 在 codeigniter 中编写,但由于单引号而出现一些错误引号 那么如何用codeigniter格式编写这个查询

Query in codeigniter:   // Not working 

$this->db->query("SELECT GROUP_CONCAT( DISTINCT CONCAT("'", REPLACE(user_id, ",", "','") , "'")) 
as listed_id FROM user_data"); //  user_id is column name

Query in phpmyadmin:   //working

GROUP_CONCAT( DISTINCT CONCAT("'", REPLACE(user_id, ",", "','") , "'"))

【问题讨论】:

    标签: php mysql codeigniter


    【解决方案1】:

    您的查询字符串无效,因为它可以被识别为字符串:

    "SELECT GROUP_CONCAT( DISTINCT CONCAT("
    

    其余的都没有意义:

    '", REPLACE(user_id, ",", "','") , "'")) as listed_id FROM user_data"
    

    当引号在同种引号内时,您应该转义:

    $this->db->query("SELECT GROUP_CONCAT( DISTINCT CONCAT(\"'\", REPLACE(user_id,
    \",\", \"','\") , \"'\")) as listed_id FROM user_data");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-30
      • 2013-11-01
      • 2018-03-14
      • 1970-01-01
      • 1970-01-01
      • 2011-03-06
      • 2023-03-29
      相关资源
      最近更新 更多