gpt4 book ai didi

mysql - 为具有多个连接的查询编写计数查询

转载 作者:行者123 更新时间:2023-11-29 09:27:37 24 4
gpt4 key购买 nike

对于 Spring JPA,我正在使用下面的 native 查询,并且需要实现分页。为此,我需要一个计数查询,它将返回以下查询返回的行。如何编写计数查询。

SELECT  count(distinct opportunity_vendors.opportunity_id) as oppcount, vendors.name , count(applications.id) as appcount
FROM public.vendors
inner join public.opportunity_vendors on vendors.id = opportunity_vendors.vendor_id
inner join public.opportunities on opportunity_vendors.opportunity_id = opportunities.id
inner join public.applications on opportunity_vendors.opportunity_id = applications.opportunity_id
group by vendors.name having length(vendors.name) > 0;```

最佳答案

你可以这样做:

select count(*) from (
SELECT count(distinct opportunity_vendors.opportunity_id) as oppcount, vendors.name , count(applications.id) as appcount
FROM public.vendors
inner join public.opportunity_vendors on vendors.id = opportunity_vendors.vendor_id
inner join public.opportunities on opportunity_vendors.opportunity_id = opportunities.id
inner join public.applications on opportunity_vendors.opportunity_id = applications.opportunity_id
group by vendors.name having length(vendors.name) > 0
) d

最诚挚的问候,
比亚尼

关于mysql - 为具有多个连接的查询编写计数查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59304826/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com