2012-04-05 75 views
0

你好我用笨的活動記錄,我想創建一個talbe加入在一個表中如何添加括號JOIN,活動記錄笨

這是我的查詢

$table = $this->mastables ['table1']; 
    $table1 = $this->mastables ['table2']; 

    $this->db->select ('a.RelationshipCategoryName,a.RelationshipCategoryID,COUNT(b.ShopRelationshipID) AS count'); 
    $this->db->from ($table . " as a"); 
    $this->db->join ($table1 . " as b", "((a.RelationshipCategoryID=b.RecieverRelationshipCategory AND b.3rdPartyID=" .$shop_id. ") OR ( a.RelationshipCategoryID=b.SenderRelationshipCategory AND b.ShopID=" .$shop_id. ")) AND b.IsDelete!=1", 'left'); 
    $this->db->where ('a.ShopID', $shop_id); 

    $this->db->where ('a.IsActive', 1); 

    if ($limit != NULL) { 
     $this->db->limit ($limit, $offset); 
    } 
    if ($oderby != NULL && $oder != NULL) { 
     $this->db->order_by ($oderby, $oder); 
    } 

    $this->db->group_by ('a.RelationshipCategoryID'); 

    $query = $this->db->get(); 
    if ($query->num_rows() > 0) { 
     return $query->result_array(); 
    } else { 
     return FALSE; 
    } 

誤差

A Database Error Occurred 

Error Number: 1064 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') OR (a.RelationshipCategoryID=b.SenderRelationshipCategory AND b.ShopID=11)) ' at line 3 

SELECT `a`.`RelationshipCategoryName`, `a`.`RelationshipCategoryID`, COUNT(b.ShopRelationshipID) AS count FROM (`mas_shop_relationship_category` as a) LEFT JOIN `mas_shop_relationship` as b ON `a`.`RelationshipCategoryID`=`b`.`RecieverRelationshipCategory` AND b.3rdPartyID=11) OR (a.RelationshipCategoryID=b.SenderRelationshipCategory AND b.ShopID=11)) AND b.IsDelete!=1 WHERE `a`.`ShopID` = '11' AND `a`.`IsActive` = 1 GROUP BY `a`.`RelationshipCategoryID` ORDER BY `a`.`RelationshipCategoryName` asc LIMIT 5 

Filename: C:\xampp\htdocs\elephanti2\system\database\DB_driver.php 

Line Number: 330 

誤差從該線到來

​​

如何在我的代碼

((a.RelationshipCategoryID=b.RecieverRelationshipCategory AND b.3rdPartyID=" .$shop_id. ") OR ( a.RelationshipCategoryID=b.SenderRelationshipCategory AND b.ShopID=" .$shop_id. ")) 
+0

看來查詢時產生從這裏被轉義2個paranthesess'只使用$this->db->query('whole sql query');( (a.RelationshipCategoryID = B'。再次檢查您的查詢。它不應該是發生 – safarov 2012-04-05 07:22:08

+0

@safarov,所以我能在這種情況下做 – 2012-04-05 07:30:40

+1

總是儘量讓核心SQL查詢,使CI查詢之前。 如果在phpMyAdmin運行那麼只能製作該查詢的CI版本 – TechCare99 2012-04-05 07:43:39

回答

0

看來查詢時產生從這裏((a.RelationshipCategoryID=b..被轉義2個paranthesess再次檢查查詢正確地添加這部分代碼。也爲db->select(),你應該爲第二個參數傳遞爲false,使無法逃避特殊字符

$this->db->select ('select query', false); 

,但它能夠更好地進行復雜查詢