codeigniter - 防止在select语句上添加空格

时间:2017-01-11 04:41:50

标签: codeigniter codeigniter-3

如何使用CodeIgniter 3阻止select语句中的自动修复。

我想要的查询:SELECT REPLACE(item, ',', '') AS item(没有空格)

我尝试了$this->db->select("replace(item, ',', '') AS item", FALSE);

结果SELECT REPLACE(item, ', ', '') AS item(CI在我的查询中添加了空格)

我看了this documentation但没有工作

这是我的真实查询

$this->db->select("a.unit_id"); 
$this->db->select("replace(c.topik, ',', '') AS topik",  FALSE); //this my problem (added space which shouldnt)
$this->db->select("COUNT(*) AS jumlah");
$this->db->from('opub_kliping a');
$this->db->join('opub_media b','a.media_id = b.organisasi_id');
$this->db->join('opub_tag c','a.id = c.kliping_id');
if(!empty($data['keyword']))$this->db->where("(a.judul LIKE '%".$data['keyword']."%' OR a.text LIKE '%".$data['keyword']."')");
if(!empty($data['tgl1']))$this->db->where("a.tanggal >=",$data['tgl1']);
if(!empty($data['tgl2']))$this->db->where("a.tanggal <=",$data['tgl2']);
if(!empty($data['unit']))$this->db->where("a.unit_id ",$data['unit']);
if(!empty($data['media']))$this->db->where("a.media_id ",$data['media']);
if(!empty($data['jenis']))$this->db->where("a.jenis_id ",$data['jenis']);
if(!empty($data['tone']))$this->db->where("a.karakter ",$data['tone']);
if(!empty($data['topik']))
{
    $qTopik = array();
    $expTopik = explode(',', $data['topik']);
    foreach($expTopik as $t)
    {
        if(!empty($t))
        {
            $qTopik[] = "c.topik LIKE '%$t%'";
        }
    }
    if(!empty($qTopik))
    {
        $qTopiks = implode(",", $qTopik);
        $this->db->where($qTopiks);
    }
}
$this->db->where('a.unit_id !=', 0);
$this->db->where('c.topik !=', NULL); 
$this->db->where('c.topik !=', '');
$this->db->group_by("a.unit_id");
$this->db->group_by("replace(c.topik, ',', '')", FALSE); //this produce correct query (without adding space)
$this->db->order_by('a.unit_id', 'ASC');
$this->db->order_by('jumlah', 'DESC');

$q= $this->db->get(); 

感谢先进的

2 个答案:

答案 0 :(得分:0)

更改查询的4行

$this->db->select("a.unit_id,replace(c.topik, ',', '') AS topik,COUNT(*) AS jumlah")->from('opub_kliping a');
$this->db->join('opub_media b','a.media_id = b.organisasi_id');

答案 1 :(得分:0)

我在尝试了一些实验后发现了这个

for ( i = j = 0 ; s[i] != '\0' ; i++) { if (s[i] != q[k]) s[++j] = s[i]; }

似乎很可能是黑客。但它的工作对我来说 希望能帮助别人