日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

laravel 中 distinct() 的使用方法與去重

作者:知其黑、受其白 更新時間: 2022-07-10 編程語言

閱讀目錄

  • 闡述
    • 一、laravel5 怎么使用 distinct
    • 二、如何select多個字段

闡述

laravel 中 distinct( ) 的使用方法與去重, MySQL 通常使用 GROUPBY (本質上是排序動作)完成 DISTINCT 操作,如果 DISTINCT 操作和 ORDERBY 操作組合使用,通常會用到臨時表,這樣會影響性能,在一些情況下, MySQL 可以使用索引優化 DISTINCT 操作,但需要活學活用。

一、laravel5 怎么使用 distinct

laravel5 使用 distinct 很簡單,官方已經提供了使用 distinct 方法允許你強制讓查找返回不重復的結果:

$users = DB::table('users')->distinct()->get();

可是官方提供的代碼是不能查找返回不重復的結果的,我們需要指定一下字段:

$users=DB::table('users')->select('name')->distinct()->get();

二、如何select多個字段

如果想要 distinct 多個字段,可以在 select 中添加字段名稱;
但是要注意,當 select 多個字段,代表需要 status 與 name 都相同的才會被排除。

$users=DB::table('users')->select('status','name')->distinct()->get();

這就是laravel中 distinct 使用方法的匯總了,如果你想要從數據庫中去重獲取數據,可以使用 GROUPBY 方法。

原文鏈接:https://blog.csdn.net/weiguang102/article/details/125507686

欄目分類
最近更新