使用Codeigniter活动记录类我正在模型中加入我的表:
$subquery =
$this->db
->from('houseconstructiontypelist')
->where('HouseFk', $id)
->where('discount_id', '0')
->get_compiled_select();
$query =
$this->constructiontype_model
->select('*, constructiontype.PkID AS ConstructionTypeID')
->join('('.$subquery.') t2', 'constructiontype.PkID = t2.ConstructionTypeFk', 'left outer')
->find_all();
return $query;
并且因为第二个表中没有与'discount_id' = 0
匹配的行,我得到了我的结果:
array (size=5)
0 =>
object(stdClass)[50]
public 'PkID' => null
public 'Name' => string 'Standard' (length=8)
public 'Descr' => string 'Standard' (length=8)
public 'HouseFk' => null
public 'ConstructionTypeFk' => null
public 'Price' => null
public 'Discount' => null
public 'Subvention' => null
public 'discount_id' => null
public 'ConstructionTypeID' => string '1' (length=1)
1 =>
object(stdClass)[51]
public 'PkID' => null
public 'Name' => string 'Prestige' (length=8)
public 'Descr' => string 'Prestige' (length=8)
public 'HouseFk' => null
public 'ConstructionTypeFk' => null
public 'Price' => null
public 'Discount' => null
public 'Subvention' => null
public 'discount_id' => null
public 'ConstructionTypeID' => string '2' (length=1)
我得到了第一个表(constructiontype)的所有结果,无论第二个表(houseconstructiontypelist)中是否有任何匹配结果,一切正常。而且总会有一个!在第二个表中匹配结果,请注意子查询的->where('discount_id', '0')
部分。
现在如果我还要加入第二个表->where_not_in('discount_id', '0')
的其他结果怎么办?但我也想保留第一个查询给我的结果!我想得到这个:
array (size=5)
0 =>
object(stdClass)[50]
public 'PkID' => null
public 'Name' => string 'Standard' (length=8)
public 'Descr' => string 'Standard' (length=8)
public 'HouseFk' => null
public 'ConstructionTypeFk' => null
public 'Price' => null
public 'Discount' => null
public 'Subvention' => null
public 'discount_id' => null
public 'ConstructionTypeID' => string '1' (length=1)
1 =>
object(stdClass)[51]
public 'PkID' => string '610' (length=3)
public 'Name' => string 'Standard' (length=8)
public 'Descr' => string 'Standard' (length=8)
public 'HouseFk' => string '135' (length=3)
public 'ConstructionTypeFk' => string '1' (length=1)
public 'Price' => string '4564' (length=4)
public 'Discount' => string '0' (length=1)
public 'Subvention' => string '0' (length=1)
public 'discount_id' => string '3' (length=1)
public 'ConstructionTypeID' => string '1' (length=1)
2 =>
object(stdClass)[51]
public 'PkID' => null
public 'Name' => string 'Prestige' (length=8)
public 'Descr' => string 'Prestige' (length=8)
public 'HouseFk' => null
public 'ConstructionTypeFk' => null
public 'Price' => null
public 'Discount' => null
public 'Subvention' => null
public 'discount_id' => null
public 'ConstructionTypeID' => string '2' (length=1)
数组(1)的第二个元素现在填充了值,因为第二个表中存在匹配,其中第一个元素(0)具有空值,因为就像我说的那样仍然没有匹配!但是我需要返回那些空值。
我尝试使用第二个子查询(使用->where_not_in('discount_id', '0')
)和另一个sql连接部分:
$subquery =
$this->db
->from('houseconstructiontypelist')
->where('HouseFk', $id)
->where('discount_id', '0')
->get_compiled_select();
$subquery2 =
$this->db
->from('houseconstructiontypelist')
->where('HouseFk', $id)
->where_not_in('discount_id', '0')
->get_compiled_select();
$query =
$this->constructiontype_model
->select('*, constructiontype.PkID AS ConstructionTypeID')
->join('('.$subquery.') t2', 'constructiontype.PkID = t2.ConstructionTypeFk', 'left outer')
->join('('.$subquery2.') t3', 'constructiontype.PkID = t3.ConstructionTypeFk', 'left outer')
->find_all();
但是这只给了我带有填充值的结果,因为它找到了匹配并且它完全忽略了第一次使用子查询->where('discount_id', '0')
的连接调用:
0 =>
object(stdClass)[51]
public 'PkID' => string '610' (length=3)
public 'Name' => string 'Standard' (length=8)
public 'Descr' => string 'Standard' (length=8)
public 'HouseFk' => string '135' (length=3)
public 'ConstructionTypeFk' => string '1' (length=1)
public 'Price' => string '4564' (length=4)
public 'Discount' => string '0' (length=1)
public 'Subvention' => string '0' (length=1)
public 'discount_id' => string '3' (length=1)
public 'ConstructionTypeID' => string '1' (length=1)
1 =>
object(stdClass)[51]
public 'PkID' => null
public 'Name' => string 'Prestige' (length=8)
public 'Descr' => string 'Prestige' (length=8)
public 'HouseFk' => null
public 'ConstructionTypeFk' => null
public 'Price' => null
public 'Discount' => null
public 'Subvention' => null
public 'discount_id' => null
public 'ConstructionTypeID' => string '2' (length=1)