需求,查询两个表, 将值相减,存入另外一张表
今天做开发,需求是
/** * 1.先获取查询lraccount账单表, * 2.查询数据预估表lrlistCollect表, * 3.二者相减存到lrpreAdjust账单调整表 * 4.发送邮件 */
初步就像写的逻辑是
String sql01="select * from Lraccount where month= "; SSRS accoutSSRS = new ExeSQL().execSQL(sql01); String sql02="select * from Lraccount where month= "; SSRS listSSRS = new ExeSQL().execSQL(sql01); /** * 取最大的循环,对每一条进行判断 * for(i,i<list){ * for(j,j<account){ * if(list.sno(i)==account.son(j){ * insert(list.sno(i)-account.son(j)); * 将account中做过处理的条目插入临时templist * break; * * } * 对于list存在,而account不存在,将list(i)存入adjust * insert(list(i) * } * } #循环结束 * 检查 account.row 和 templist.row * if(account.row>templist.row){ * 将 for(i<account){ * if(acount.sno(i)==templist.sno(i){ * continue; * }else{ * insert(account(i)); * } * 数据处理完毕 * 发送邮件 * System.out.println(tSSRS.GetText(index, 3)+":::"+exeSql); */ // listSSRS 和 accoutSSRS List templist = new ArrayList();//临时数据表 if(listSSRS==null || listSSRS.MaxRow<=0){ if(accoutSSRS==null || accoutSSRS.MaxRow<=0){ }else{ for(int index=1;index<=accoutSSRS.MaxRow;index++){ System.out.println(accoutSSRS.GetText(index, 3)); //处理之后,insert(); } } }else{ if(accoutSSRS==null || accoutSSRS.MaxRow<=0){ for(int index=1;index<=listSSRS.MaxRow;index++){ System.out.println(listSSRS.GetText(index, 3)); //处理之后,insert(); } }else{ for(int i=1;i<=listSSRS.MaxRow;i++){ for(int j=1;j<=accoutSSRS.MaxRow;j++){ if(listSSRS.GetText(i, 1)==accoutSSRS.GetText(j, 1)){ //相减,做插入操作 templist.add(accoutSSRS.GetText(j,1)); }else{ //list的数据做插入操作 } } } //处理account中的数据 if(accoutSSRS.MaxRow>templist.size()){ for(int i=1;i<=accoutSSRS.MaxRow;i++){ if(templist.contains(accoutSSRS.GetText(i,1))){ }else{ //accountSSRS数据做负数处理,再insert()操作; } } } } }
觉得逻辑好复杂,就像有简单的方法吗?自己想出来是使用sql语句进行逻辑判断和运算
SELECT a.`list_id` AS id, a.`unicode` , (IFNULL(a.`value`,0) - IFNULL(b.`value`,0)) AS dif FROM LIST a LEFT JOIN account b ON a.unicode = b.`unicode` UNION SELECT b.`account_id` AS id, b.`unicode` , (IFNULL(a.`value`,0) - IFNULL(b.`value`,0)) AS dif FROM LIST a RIGHT JOIN account b ON a.unicode = b.`unicode`
其中, list 和account 表分别为