代码之家  ›  专栏  ›  技术社区  ›  max234435

Laravel聚合雄辩的最大结果

  •  0
  • max234435  · 技术社区  · 8 年前

    以下是我想做的:

    我正在供应商表中实现version\u id。我正在尝试构造一个查询以使索引页正常工作。因此,我希望从我的数据库中获取所有具有最新version\u id(max('version\u id')的供应商。似乎无法理解。以下是我目前掌握的情况:

    $vendors = $this->account->vendors()->where('version_id', max(version_id))
    
            ->orderBy($this->orderBy, $this->sortBy)
            ->paginate($this->display_per_page);
    

    我以前试过这个,但它也会给我错误:

    $vendors = $this->account->vendors()->max('version_id')
    
                ->orderBy($this->orderBy, $this->sortBy)
                ->paginate($this->display_per_page);
    

    如果我有这个就行了:

    $vendors = $this->account->vendors()
                ->orderBy($this->orderBy, $this->sortBy)
                ->paginate($this->display_per_page);
    

    我一直在网上寻找,但我只找到关于连接等查询的建议,这不是我想要的。感谢您的帮助!

    1 回复  |  直到 8 年前
        1
  •  2
  •   DevK    8 年前

    这应该可以:

    $vendors = $this->account->vendors()
        ->whereRaw('version_id = (SELECT MAX(version_id) from vendors)')
        ->orderBy($this->orderBy, $this->sortBy)
        ->paginate($this->display_per_page);
    

    假定表名 vendors

    推荐文章