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

从  一个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());
	}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值