我想进行如下查询:

SELECT count(DISTINCT mobile)
FROM qa_account.customer_contact
WHERE customer_id=10001;


在crudRepository中,但也得到包括重复项在内的总数。
我不想编写本机查询。

请给我一些方法来实现我的要求。

最佳答案

如果您想区分整个SQL行,则可以执行以下操作

// Spring Data allows you to specify the entity you want to distinct after the word "Distinct"
List<CustomerContact> findDistinctMobileByCustomerId();

// Or before the word "Distinct"
List<CustomerContact> findMobileDistinctByCustomerId();


如何获得不同的计数

List<CustomerContact> listCustomerContact = yourRepository.findDistinctMobileByCustomerId(10001)
listCustomerContact.size();


我假设你有这样的数据

CUSTOMER_ID MOBILE

10001 ________ A

10001 ________ A

10001 ________ B

10001 ________ C

结果输出计数不同的移动设备= 3

有关此的更多信息,您可以登录Spring Data Documentation

关于mysql - Spring 应用程序中的crudRepository无法使用不同的移动计数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50405576/

10-10 09:07