SQL 从一个sql 语句结果中(作为AS一个表) 查询结果 ;按照count排序

来源:互联网 发布:win10怎么连接有线网络 编辑:程序博客网 时间:2024/06/01 16:16

从  一个sql 语句中查找另一个sql语句查询的结果:


public void  getSumCountgsMebDTO1(int memberSum){SQLiteDatabase db = helper.getReadableDatabase();String COUNT_NUM="COUNT_NUM";String sql1 ="select * from "+ "(select  "+DBConstants.DB_SEND_GROUP_MB_groupsendid+","+ " count("+DBConstants.DB_SEND_GROUP_MB_groupsendid+ ") "+ " as "+ COUNT_NUM+ " from " + DBConstants.DB_SEND_GROUP_MB+ " group by "+DBConstants.DB_SEND_GROUP_MB_groupsendid+") as tabl2 "//内部用不了COUNT_NUM,外部可以使用+ " where tabl2.COUNT_NUM = "+3;//String sql1 = "select  "+DBConstants.DB_SEND_GROUP_MB_groupsendid+","//+ " count("+DBConstants.DB_SEND_GROUP_MB_groupsendid+ ") "//+ " as "+ COUNT_NUM//+ " from " + DBConstants.DB_SEND_GROUP_MB//+ " group by "+DBConstants.DB_SEND_GROUP_MB_groupsendid//+ " order by "+ " count("+DBConstants.DB_SEND_GROUP_MB_groupsendid+ ") "+"  desc";Cursor cursor  = db.rawQuery(sql1, null);MyLog.e(TAG, " getSumCountgsMebDTO cursor.count =  "+cursor.getCount());StringBuffer sb=new StringBuffer();while (cursor.moveToNext()) {int num = cursor.getInt(cursor.getColumnIndex(COUNT_NUM));//String memberNum = cursor.getString(cursor.getColumnIndex(DBConstants.DB_SEND_GROUP_MB_membernumber));String groupsendid = cursor.getString(cursor.getColumnIndex(DBConstants.DB_SEND_GROUP_MB_groupsendid));sb.append(" getSumCountgsMebDTO result = "//+ memberNum +"  "+groupsendid+" = "+num+";\n");}MyLog.e(TAG, sb.toString());}

=============获取 某列 在从sqlite语句中查询的结果的cursor当中,===============================================================



/**按照群发组分类: 每个群发组有多少成员;*/public void  getSumCountgsMebDTO(ArrayList<GroupSendMemberDTO> gsmdtos){/** * 分析: * 1.gsmdtos 就是当前的群发的成员结果集合; * 2.如果gsmdtos  不包含cursor.get(index),则去除这个groupSendid(将其纪录下来,下次的groupSendid包含则跳过) * 3.如果都包含,则去取出这个groupSendid * 4.通过groupsendid,查询groupSenddto,和groupsendMsg */if (gsmdtos==null||gsmdtos.size()<1) {throw new IllegalArgumentException(" getSumCountgsMebDTO2(ArrayList<GroupSendMemberDTO> gsmdtos) 的gsmdto 的参数异常");}SQLiteDatabase db = helper.getReadableDatabase();String COUNT_NUM="COUNT_NUM";String TABLE2="TABLE2";String sql1 ="select * from " + DBConstants.DB_SEND_GROUP_MB+ " where "+DBConstants.DB_SEND_GROUP_MB_groupsendid+" in "+"(select "+DBConstants.DB_SEND_GROUP_MB_groupsendid+" from "+ "(select "+DBConstants.DB_SEND_GROUP_MB_groupsendid+","+ " count("+DBConstants.DB_SEND_GROUP_MB_groupsendid+ ") "+ " as "+ COUNT_NUM+ " from " + DBConstants.DB_SEND_GROUP_MB+ " group by "+DBConstants.DB_SEND_GROUP_MB_groupsendid+") as "+TABLE2//内部用不了COUNT_NUM,外部可以使用+ " where "+TABLE2+"."+COUNT_NUM+" = "+gsmdtos.size()+" ) ";//String sql1 = "select  "+DBConstants.DB_SEND_GROUP_MB_groupsendid+","//+ " count("+DBConstants.DB_SEND_GROUP_MB_groupsendid+ ") "//+ " as "+ COUNT_NUM//+ " from " + DBConstants.DB_SEND_GROUP_MB//+ " group by "+DBConstants.DB_SEND_GROUP_MB_groupsendid//+ " order by "+ " count("+DBConstants.DB_SEND_GROUP_MB_groupsendid+ ") "+"  desc";Cursor cursor  = db.rawQuery(sql1, null);ArrayList<String> cursorGrpSendIds=new ArrayList<String>();MyLog.e(TAG, " getSumCountgsMebDTO cursor.count =  "+cursor.getCount());StringBuffer sb=new StringBuffer();while (cursor.moveToNext()) {String memberNum = cursor.getString(cursor.getColumnIndex(DBConstants.DB_SEND_GROUP_MB_membernumber));String groupsendid = cursor.getString(cursor.getColumnIndex(DBConstants.DB_SEND_GROUP_MB_groupsendid));sb.append(" getSumCountgsMebDTO result = "+groupsendid+" = "+memberNum+";\n");}MyLog.e(TAG, sb.toString());}





阅读全文
0 0