一个总表,一个详情表,总表的单位和详情表的品目一起做组队。但是最后的结果里面还是有单个的结果,最后我也count(1)>1了为什么还是会有单条的语句呢?
********SQL*******
select * from gfm_gp_purplan_bill T where exists(
select p.agency_code,d.pur_item_code,count(1),sum(d.amount),sum(p.amount) from gfm_gp_purplan_bill p left join gfm_gp_purplan_detail d on p.purplan_id=d.purplan_id
where p.pur_met_code!='1' and p.status>=4 and p.is_valid=1 and p.type<=3 and p.is_preyear=0 and p.amount group by p.agency_code,d.pur_item_code
having sum(p.amount)>=2000000 and count(1)>1
);
EXISTS用于检查子查询是否至少会返回一行数据,该子查询实际上并不返回任何数据,而是返回值True或False
exists 只是一个结果判断,影响输入数据的范围,不会影响select * from gfm_gp_purplan_bill T 的输出 的数据结果
exists 只是判断是否存在,满足的话,就取记录,只要一笔即可;不满足,就不取。COUNT,GOUPY BY要放外面的。
exits 的运算规则是,从外层查询中拿到一行数据,进入到内层查询中进行判断,如果符合内层查询 也就是 符合exists的条件,那么就进行输出,否则不输出
详见 https://blog.csdn.net/qq_36437446/article/details/80636617
因为你exists 里面 没有关联t 少了t.字段=p.字段 如果还不行可以联系我qq434317590