Php 如何在表联接中添加偏执,活动记录Codeigniter

Php 如何在表联接中添加偏执,活动记录Codeigniter,php,mysql,codeigniter,activerecord,Php,Mysql,Codeigniter,Activerecord,嗨,我正在使用codeigniter活动记录,我想创建一个talbe连接 这是我的问题 $table = $this->mastables ['table1']; $table1 = $this->mastables ['table2']; $this->db->select ( 'a.RelationshipCategoryName,a.RelationshipCategoryID,COUNT( b.ShopRelationshipID ) A

嗨,我正在使用codeigniter活动记录,我想创建一个talbe连接

这是我的问题

    $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
错误来自这一行

    $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' );
如何在我的代码中正确添加这部分代码

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

似乎当查询生成从此处转义的2个Paranthesses时,
((a.RelationshipCategoryID=b..
再次检查您的查询。对于
db->select()
,您应该将其作为第二个参数传递为
false
,以避免转义特殊字符

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

但是最好只使用
$this->db->query('whole sql query');
用于复杂查询

当query生成2个从这里转义的Paranthesses时,
((a.RelationshipCategoryID=b
。请再次检查您的查询。它不应该是happen@safarov,那么在这种情况下我能做什么呢?在进行CI查询之前,我会尝试进行核心SQL查询。如果它在phpMyAdmin中运行,则只进行该查询的CI版本