NutzCN Logo
问答 count查询为空或者为0的时候,发现下面两种写法都是返回1,但是直接把sql放在mysql里面查询是0,难道是用了union导致的
发布于 2702天前 作者 qq_710cab78 6647 次浏览 复制 上一个帖子 下一个帖子
标签:

String countstr="sql语句请看下面";
Sql countSql = Sqls.create(countstr);
countSql.setCallback(Sqls.callback.longValue());
dao().execute(countSql);
Long totalRecordCount = countSql.getObject(Long.class);

Sql countSql = Sqls.create(countstr);
countSql.setCallback(Sqls.callback.longValue());
Long totalRecordCount = Daos.queryCount(dao(), countSql);
count查询为空或者为0的时候,发现上面两种写法都是返回1,但是直接把sql放在mysql里面查询是0,难道是用了union导致的??

select ifnull(count(*),0) from ( select * from (select c.code  from T_crm_contract c where  1=1  and ( c.code like '%SHOKY20141201-00021#001%' or c.name like '%SHOKY20141201-00021#001%')  
and c.status<>'9' and c.inner_flag<>'1'  and (  ( (c.amount<>c.v_pay_amount and c.amount>0) OR (c.v_invoice_amount<>(c.v_pay_amount-c.v_rebate_amount)
 and c.v_invoice_amount<>c.v_pay_amount) )  OR (c.v_deposit_plan_amount>0 and (c.v_deposit_plan_amount<>c.v_deposit_map_amount OR c.v_deposit_cantake_amount>0))
  OR (c.v_advance_plan_amount>0 and (c.v_advance_plan_amount<>c.v_advance_map_amount OR c.v_advance_plan_amount<>c.v_advance_invoice_map_amount OR c.v_advance_cantake_amount>0))
  OR (v_rebate_plan_amount>0 and (c.v_rebate_map_amount<>c.v_rebate_invoice_map_amount OR c.v_rebate_cantake_amount>0))
  OR (c.process_status<>'流程结束' OR c.process_status is null OR c.archive_uid is null)  )  ) t3
union  
select * from  (select t.ok_crm_code from T_crm_order t 
where t.single_check is null  and ( t.ok_crm_code like '%SHOKY20141201-00021#001%' or t.order_title like '%SHOKY20141201-00021#001%' or t.contract_code like '%SHOKY20141201-00021#001%')
  and t.status<>'9' and t.inner_flag<>'1'  and (  ((t.amount<>t.v_pay_amount and t.amount>0) OR (t.v_invoice_amount<>(t.v_pay_amount-t.v_rebate_amount) and t.v_invoice_amount<>t.v_pay_amount) ) 
 OR (t.process_status<>'流程结束' OR t.process_status is null OR t.archive_uid is null)  )  ) t5  ) aa;
3 回复

刚试了一下,这条语句又可以,下面也是为空,默认给0,查出来totalRecordCount 又是0。
select ifnull(count(*),0) from ( select c.code from T_crm_contract c where c.code='ddd' union select t.ok_crm_code from T_crm_order t where t.ok_crm_code='dddd' ) aa;

Sqls.callback.longValue() 是取ResultSet的第1个类的值, 你可以自定义一个回调看看有多少列的数据

添加回复
请先登陆
回到顶部