2012-04-05 3 views
0

hallo i aktive Aufzeichnungen sind mit codeigniter, iwie Klammern in einer Tabelle hinzufügen JOIN Aktive Aufzeichnungen Codeigniter

ein talbe beitreten erstellen mag dies ist meine Abfrage

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

der Fehler

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 

die Fehler aus dieser Linie kommen

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

, wie dieser Teil des Codes richtig in meinem Code hinzufügen

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

Es scheint, als Abfrage generieren 2 paranthesess von hier 'entkam werden ((a.RelationshipCategoryID = b'. Überprüfen Sie Ihre Abfrage erneut. Es sollte nicht geschehen – safarov

+0

@safarov, also was kann ich in dieser Situation tun –

+1

Immer versuchen, Kern SQL-Abfrage zu machen, bevor Sie CI-Abfrage. , wenn es in phpMyAdmin ausgeführt wird Machen Sie dann nur die CI-Version dieser Abfrage – TechCare99

Antwort

0

Es scheint, als Abfrage 2 erzeugen paranthesess ((a.RelationshipCategoryID=b.. wieder Ihre Anfrage Überprüfen von hier entkommen ist. Auch für db->select() sollten Sie als zweiter Parameter als false passieren zu keinen Sonderzeichen machen entkommen

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

aber es ist besser zu nutzen nur $this->db->query('whole sql query'); für komplexe Abfragen

Verwandte Themen