php - 如何从codeigniter中的多行获取相同的foreign_key记录
问题描述
我正在尝试在数组中获取具有相同 ID 的记录。我尝试了“Group_by”查询,但结果为单个数组。请参阅下图中的记录。
询问:
$this->db->select('typeID,projectID,typeName,typeSize,dimensionWidth,dimensionHeight');
$this->db->from('propertytype');
$this->db->group_by('projectID');
$rec=$this->db->get()->result();
echo "<pre>";print_r($rec);exit();
结果:
Array
(
[0] => stdClass Object
(
[typeID] => 1
[projectID] => 1
[typeName] => Residential
[typeSize] => 5 MARLA
[dimensionWidth] => 125
[dimensionHeight] => 125
)
[1] => stdClass Object
(
[typeID] => 7
[projectID] => 2
[typeName] => Residential
[typeSize] => 5 MARLA
[dimensionWidth] => 26
[dimensionHeight] => 50
)
)
我想要二维数组中的结果。
Array(
[0]=> stdClass object(
Array(...)
Array(...)
Array(...)
Array(...)
)
[1]=> stdClass object(
Array(...)
Array(...)
Array(...)
Array(...)
)
)
告诉我我在哪里犯了错误?任何人都可以帮助我。我将非常感激。
解决方案
该GROUP BY
子句通常用于Aggregate Functions
返回每个组的汇总值。没有聚合的GROUP BY
子句类似于 using SELECT DISTINCT
。...该GROUP BY
子句不对数据进行排序。
我希望这可以帮助你:
$this->db->select('projectID');
$this->db->from('propertytype');
$this->db->distinct('projectID');
$rec=$this->db->get()->result();
$arr=array();
foreach ($rec as $r) {
$this->db->select('typeID,projectID,typeName,typeSize,dimensionWidth,dimensionHeight');
$this->db->from('propertytype');
$this->db->where('projectID',$r->projectID);
$rec=$this->db->get()->result();
$arr[]=$rec;
}
echo "<pre>";print_r($arr);exit();