LambdaQueryWrapper实现distinct
时间: 2023-10-17 19:36:12 浏览: 222
MySQL DISTINCT 的基本实现原理详解
LambdaQueryWrapper可以通过使用方法`select`和`groupBy`来实现distinct查询。通过在`select`方法中指定需要查询的字段,可以消除重复记录。例如,使用LambdaQueryWrapper实现distinct查询如下所示:
```java
List<Student> list = studentMapper.selectList(Wrappers.<Student>lambdaQuery()
.eq(Student::getStudentId, list.get(0).getId())
.eq(Student::getUserId, userId)
.eq(Student::getStatus, 1)
.eq(null != termId, Course::getTermId, termId)
.orderByDesc(Course::getCreateTime)
.select(Student::getName)
.groupBy(Student::getName));
```
在上面的例子中,通过在`select`方法中指定`Student::getName`,可以实现对`name`字段的distinct查询。同时,使用`groupBy`方法对`name`字段进行分组,以确保只返回不重复的记录。这样就可以实现LambdaQueryWrapper的distinct查询。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [MybatisPlus lambdaQueryWrapper中常用方法](https://blog.csdn.net/moyingmj/article/details/121796354)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *3* [oracle sql 去重复记录不用distinct如何实现](https://download.csdn.net/download/weixin_38625464/14837268)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文