Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
codeigniter活动记录聚合不工作_Codeigniter_Activerecord_Codeigniter 3 - Fatal编程技术网

codeigniter活动记录聚合不工作

codeigniter活动记录聚合不工作,codeigniter,activerecord,codeigniter-3,Codeigniter,Activerecord,Codeigniter 3,我使用的是Codeigniter,我要做的是返回联接表(item_gallery)gallery_id为最低值的查询。而主查询则是按物料的描述排序的过账日期 下面的代码通过选择项目\u库\u id的随机值对\u进行分组。但我需要项目\u库\u id的最低值 public function shopItems($id) { $this->db->select("*,MIN(item_gallery.gallery_id)"); $this->db->f

我使用的是Codeigniter,我要做的是返回联接表(item_gallery)gallery_id为最低值的查询。而主查询则是按物料的描述排序的过账日期

下面的代码通过选择项目\u库\u id的随机值对\u进行分组。但我需要项目\u库\u id的最低值

  public function shopItems($id) {

    $this->db->select("*,MIN(item_gallery.gallery_id)");
    $this->db->from('items');
    $this->db->join('item_gallery', 'items.id = item_gallery.item_id', 'left');
    $this->db->where('items.user_id', $id);     
    $this->db->order_by('items.post_date', 'Desc');
    $this->db->group_by("item_gallery.item_id");
    $query = $this->db->get();
    return $query->result();
}
数据库结构

项目表

|    id    |  slug  |  user_id  |  post_date |

|    12    |  test  |    111    |  12/5/2017 |
项目库

| gallery_id |  item_id  |     image     | 

|     121    |    12     |  profile.png  |   -- i want this record selected
|     122    |    12     |  gallery.png  | 

您是否尝试过在联接条件中使用内部查询进行MIN

    $this->db->select("*,MIN(item_gallery.gallery_id)");
    $this->db->from('items');
    $this->db->join('item_gallery', 'items.id = ( SELECT MIN(item_gallery.gallery_id) id FROM item_gallery WHERE items.id = item_gallery.item_id )', 'left');
    $this->db->where('items.user_id', $id);
    $this->db->order_by('items.post_date', 'Desc');
    $this->db->group_by("item_gallery.item_id");
    $query = $this->db->get();

不返回图像。更改$this->db->select(,MIN(item\u gallery.gallery\u id));要$this->db->选择(“items.,item_gallery.image”);添加上述代码后也不起作用,当有3个项目时,此代码会出现其他问题仅显示一个图像不可见的项目刚刚测试了代码。对我来说,它工作正常(第一个答案)。这将仅返回项目库中的一行(项目库库id的最低值)以及相关项目行。如果需要所有匹配的行,则需要更正该问题。