如何向 CodeIgniter 查询添加 LIKE 条件?

问题描述 投票:0回答:4

我有一个包含以下几行的 CodeIgniter 查询。

$userloc = $this->session->userdata('location');
$query = $this->db->query('
    select assettype,
           sum(status="In Stock")In_stock,
           sum(status = "Allocated")Allocated,
           sum(status = "Decommission")Decommission,
           sum(status = "In transit")In_Transit,
           location
    from assets
    group by assettype, location
');

我想在

$query
处放置一个 LIKE 条件,其中
location LIKE $userloc
,但我的尝试没有成功。

php mysql codeigniter sql-like
4个回答
1
投票

试试这个:

$userloc = $this->session->userdata('location');

$query = $this->db->query('select assettype, sum(status="In Stock") In_stock,
sum(status = "Allocated") Allocated,
sum(status = "Decommission") Decommission,
sum(status = "In transit") In_Transit,
location from assets where  location like "%'.$userloc.'%" group by assettype,location');

0
投票

尝试使用以下

$this->db->select('assettype,sum(status="In Stock")In_stock,sum(status =  "Allocated")Allocated,sum(status = "Decommission") Decommission,sum(status = "In transit")In_Transit,location');
$this->db->from('assets');
$this->db->like('location', $userloc);
$this->db->group_by('assettype,location');
$query = $this->db->get();

0
投票

尝试以下查询:

$query=$this->db->query('select assettype,sum(status="In Stock")In_stock,sum(status = "Allocated")Allocated,sum(status = "Decommission")Decommission,sum(status = "In transit")In_Transit,location from assets where location like %$userloc% group by assettype,location');


0
投票

使用以下查询

$userloc=$this->session->userdata('location');

$query=$this->db->query('select assettype,sum(status="In Stock")In_stock,sum(status = "Allocated")Allocated,sum(status = "Decommission")Decommission,sum(status = "In transit")In_Transit,location from assets where location like '%$userloc%' group by assettype,location');
© www.soinside.com 2019 - 2024. All rights reserved.