2011-11-29 86 views
0

這是我的MySQL查詢MySQL查詢在笨

$acountry = 1; 
$this->db->where_in('varcountry', $acountry); 
$val = $this->db->get('tblagencies')->result(); 

在數據庫表提交存儲這樣的varcountry 1,2,3,4其類型是varchar.Each行表有多個國家的是使用varchar數據類型的原因。

在這裏,我想選擇表中有$acountry值的錶行。

我該怎麼做?以上代碼是否正確?

回答

1

你已經選擇了一個錯誤的數據類型,用於存儲逗號分隔值1,2,3,4爲VARCHAR,
你應該選擇一個data-type of set,或歸到一個單獨的表,如: -

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 

使用一套更容易,但通常的做法是對數據進行標準化(需要一些理解)。

我不喜歡笨的活動記錄,
易於使用(不帶着這個疑問),
但DIS-允許丟失的靈活性

個人而言,我喜歡我的構建自己的查詢,
只要你有表模式的理解(你必須反正)

0

使用此查詢..

$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);