2011-05-30 94 views
8

如何在Codeigniter的活動記錄SQL查詢中使用圓括號符號? 例如如何實現Codeigniter - 帶有activerecord的括號?

SELECT * FROM `shops` WHERE (`shopid` = '10' OR `shopid` = '11') AND `shopid` <> '18' 
+0

在CI3.0你可以做到這一點http://www.codeigniter.com/userguide3/database/query_builder .html#query-grouping – Calvin 2016-11-11 04:01:10

回答

5

我認爲你可以這樣做:

$where = "(`shopid` = '10' OR `shopid` = '11')"; 

$this->db->where($where) // or statement here 
     ->where('shopid <>', '18') // chaining with AND 
     ->get('shops'); 

不能完全確定有關語法,我在寫這從我的頭頂。如果這不起作用,我會在家時看看它。

+0

這是爲什麼downvoted? – cabaret 2012-04-15 16:04:09

+0

這有助於我感謝你。 – busycoding 2012-11-29 18:51:34

1

你可以做類似:

$this->db->select('field')->from('shops')->where("(`shopid` = '10' OR `shopid` = '11'")->where("`shopid` <> '18'"); 
$query = $this->db->get(); 

你可以寫你自己的條款手動 :

$哪裏=「NAME = '喬' 和狀態= '老闆' OR狀態=「有效」「;

$ this-> db-> where($ where);

來源:http://www.codeignitor.com/user_guide/database/active_record.html#where

11
$where="(`shopid` = '10' OR `shopid` = '11')"; 

$這 - > DB->其中($其中,NULL,FALSE);

和AND條件使用

$this->db->where('shopid <>', '18') 

$where="(`shopid` = '10' OR `shopid` = '11')"; 
$this->db->where($where, NULL, FALSE); 
$this->db->where('shopid <>', '18') 
1
$this->db->select() 
     ->from('shops') 
     ->where("'(`shopid` = '10' OR `shopid` = '11')") 
     ->where('shopid !=', 18); 

$this->db->get()->result();