gpt4 book ai didi

mysql - 在 SQL 中使用having

转载 作者:行者123 更新时间:2023-11-29 07:55:52 25 4
gpt4 key购买 nike

我想合并以下查询以获得我想要的答案。但我好像不太明白。

查询 1:

select a.idlisting, count(p.idpostulant) as Total_postulants
from postulations p
inner join listings a on p.idlisting = a.idlisting
where a.idate >= '2014-07-01'
and a.idate < '2014-08-01'
group by a.idlisting
having total_postulants > 500

根据第一个查询得到的答案,我完成了 idlisting 子句

**Query 2:**

select count(distinct p.idpostulant)
from postulations p
inner join listings a on p.idlisting = a.idlisting
where a.idlisting in ('1001972582',
'1001964448',
'1001926936')

答案应该是订阅拥有超过 500 个假设的所有列表唯一假设的数量。

请告诉我是否可以理解。

感谢您的帮助。

最佳答案

只需使用join 和子查询:

select count(distinct p.idpostulant)
from postulations p inner join
listings a
on p.idlisting = a.idlisting inner join
(select a.idlisting, count(p.idpostulant) as Total_postulants
from postulations p inner join
listings a
on p.idlisting = a.idlisting
where a.idate >= '2014-07-01' and a.idate < '2014-08-01'
group by a.idlisting
having total_postulants > 500
) filter
on a.idlisting = filter.idlisting;

编辑:

您可以通过删除外部查询中的连接之一来稍微简化上述查询:

select count(distinct p.idpostulant)
from postulations p inner join
(select a.idlisting, count(p.idpostulant) as Total_postulants
from postulations p inner join
listings a
on p.idlisting = a.idlisting
where a.idate >= '2014-07-01' and a.idate < '2014-08-01'
group by a.idlisting
having total_postulants > 500
) filter
on p.idlisting = filter.idlisting;

关于mysql - 在 SQL 中使用having,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25190766/

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