将Arraylist传递给SQL查询以获得所需的结果。

问题描述 投票:0回答:1

我写了一个类似这样的查询,但这只返回idp列表中的agent_id,我也想得到列表中的id,但不在demo.table中,并返回count为0。

select agent_id,count(id) as agentCount from demo.table lst where lst.agent_id in(:idpList) group by agent_id order by agentCount ASC;

但这只返回idp列表中的agent_id,我也想得到列表中的id,但不在demo.table中,并返回count为0.我不知道该怎么做。

例如:List : [20,17,16,15,50]所以50不是表,但我希望50是包括在最终结果。

我有两个DB,从一个DB中获取所有的agent_id并将其存储在List中,而在另一个DB中,我有一个表,存储了与这些id相关联的任务的id和no,所以我想检查与这些agent_id相对应的任务被创建了多少次,并获取计数,但由于这些agent_id中有些存在于第二个DB表中,但有些不存在,所以对于那些不存在的计数应该返回0,但从这个查询中,我只得到了那些存在于第二个DB表中的计数。

希望能得到帮助。

java sql database parameters hql
1个回答
0
投票

试试这个。

select count(*),A.agent_id from 
(
select 20 as agent_id
union select 17
union select 16
union select 15
union select 50 ) as A  LEFT JOIN demo.table s on s.agent_id = A.agent_id where s.agent_id in(:idpList)  group by s.agent_id;  

但你必须手动创建这部分(select 20 as agent_id union select 17 union select 16 union select 15 union select 50) 的查询。

否则就没有办法了。

© www.soinside.com 2019 - 2024. All rights reserved.