Mysql 具有多个联接的查询的写入计数查询
对于SpringJPA,我使用下面的本机查询,需要实现分页。为此,我需要一个count查询,它将返回由below查询返回的行。如何编写计数查询Mysql 具有多个联接的查询的写入计数查询,mysql,sql,postgresql,spring-data-jpa,Mysql,Sql,Postgresql,Spring Data Jpa,对于SpringJPA,我使用下面的本机查询,需要实现分页。为此,我需要一个count查询,它将返回由below查询返回的行。如何编写计数查询 SELECT count(distinct opportunity_vendors.opportunity_id) as oppcount, vendors.name , count(applications.id) as appcount FROM public.vendors inner join public.opportunity_ven
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;
) t
您可以这样做:
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
致以最良好的祝愿,
比亚尼