当前位置:网站首页>laravel 中 distinct() 的使用方法与去重
laravel 中 distinct() 的使用方法与去重
2022-06-29 06:48:00 【知其黑、受其白】
阐述
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 方法。
边栏推荐
- Detailed explanation of top and free commands
- Product security - small vulnerabilities cause big problems
- MongoDB-使用mongo/mongosh命令行连接数据库
- [Kerberos] analysis of Kerberos authentication
- Up and down transitions in polymorphism
- 打包时提示: Property ‘sqlSessionFactory‘ or ‘sqlSessionTemplate‘
- RobotFramework学习笔记:Robot Framework和BrowserLibrary(PlayWright)简介
- Basic syntax - bit operation
- C actual combat - high configuration version of Snake game design
- js实现图片懒加载的一个详细方案(引入即可使用)
猜你喜欢

Prompt during packaging: property 'sqlsessionfactory' or 'sqlsessiontemplate'‘

SizeBalanceTree

Software testing

手把手系列---安装SpotBugs、并快速上手使用
![[industrial control old horse] detailed explanation of the design scheme of the running lamp control system based on Siemens S7-200PLC](/img/c5/9383a02050c83b26bb56f4045b0774.png)
[industrial control old horse] detailed explanation of the design scheme of the running lamp control system based on Siemens S7-200PLC

Interviewer: why does database connection consume resources? Where are the resources consumed?

VSLAM特征之线特征&面特征

【kerberos】kerberos 认证浅析

Detailed explanation of top and free commands

打包时提示: Property ‘sqlSessionFactory‘ or ‘sqlSessionTemplate‘
随机推荐
Prompt during packaging: property 'sqlsessionfactory' or 'sqlsessiontemplate'‘
Thread pool operations in cartographer
jsp学习部分
阿里的211是指什么?
在colaboratory上云端使用GPU训练(以YOLOv5举例)
多态中的向上和向下转型
1031 Hello World for U
Detailed design of PLC program control system for washing machine
Process communication - Pipeline
[old horse of industrial control] detailed explanation of Siemens PLC s7-300scl programming
C实战——高配版贪吃蛇游戏设计
C # import CSV into MySQL database
Sonic communication - streaming data processing - window alignment
【深度之眼吴恩达第四期作业班】多元线性回归Linear Regression with multiple variables总结
反思 - 完美主义
C mqtt subscription message
互联网公司的组织结构与产品经理岗位职责是什么?
Binary search tree
SQL SERVER 2008 发布订阅到SQL Server 2017避坑指南
Django - installing mysqlclient error: mysqlclient 1.4.0 or newer is required; you have 0.9.3