Php codeigniter中的Mysql查询

Php codeigniter中的Mysql查询,php,mysql,codeigniter,Php,Mysql,Codeigniter,这是我的mysql查询 $acountry = 1; $this->db->where_in('varcountry', $acountry); $val = $this->db->get('tblagencies')->result(); 在数据库表中,varcountry字段存储如下1,2,3,4,其类型为varchar。表中的每一行都有多个国家,这就是使用varchar数据类型的原因 在这里,我想选择在varcountry字段中具有$accountry值的表

这是我的mysql查询

$acountry = 1;
$this->db->where_in('varcountry', $acountry);
$val = $this->db->get('tblagencies')->result();
在数据库表中,
varcountry
字段存储如下1,2,3,4,其类型为varchar。表中的每一行都有多个国家,这就是使用varchar数据类型的原因

在这里,我想选择在
varcountry
字段中具有
$accountry
值的表行


我该怎么做?上面的代码正确吗?

您选择了错误的数据类型,用于将逗号分隔的值
1,2,3,4
存储到varchar,
您应该选择一个表,或将其规范化为一个单独的表,如:-

create table country (id, name ...);
create table agencies_country ( agency_id, country_id);
insert into agencies_country (agency_id, country_id)
values (x,1), (x,2), (x,3), (x,4);
// meaning 1,2,3,4 = 4 rows

// grabbing result using inner join
使用set更容易,但通常的做法是规范化数据(这需要一些理解)

我不喜欢codeigniter中的活动记录,
易于使用(这一点毋庸置疑),
但它不允许失去灵活性

就我个人而言,我喜欢构建我自己的查询,
如果您了解表模式(无论如何都必须了解),请使用此查询

$search_field = array('varcountry'=>$acountry)
$result = $this->db->get_where('tblagencies' , $search_field );

but in codeignator you can use your own queries like

$sql = "select * from tblagencies where varcountry like '%acountry%'";
$result = $this->db->query($sql);