NutzCN Logo
问答 问下自定义sql 查询可以用Cnd类
发布于 2586天前 作者 少年不再年少 1832 次浏览 复制 上一个帖子 下一个帖子
标签:
select * from t_activating_quantity   where  q_createtime in (select max(q_createtime) from t_activating_quantity   where t_activating_quantity.q_apkId=1  AND t_activating_quantity.q_deviceId=109   )

6 回复
可以的,文档有的

Sql sql = Sqls.create("select * from t_activating_quantity   where  q_createtime in (select max(q_createtime) from t_activating_quantity  $cnd )");
sql.setVar("cnd", Cnd.where("t_activating_quantity.q_apkId", "=", 1).and("t_activating_quantity.q_deviceId", "=", 109));
问下 我获取最新的日期记录  为什么还是比较之前的日期
方法代码如下:
public Result ClientInfo(@Param("json") String json, HttpServletRequest request) throws Exception {
		json = Data.decode(json);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		int num = 1;
		// GSON直接解析成对象
		UpdateClientInfo.R updateClientInfo = new Gson().fromJson(json, UpdateClientInfo.R.class);
		// 对象中拿到集合
		List<Statistics> statistics = updateClientInfo.getUserDate();
		Cnd cn2 = null;
		for (Statistics statistic : statistics) {
			cn2 = Where.where(cn2, "c_package_name", "=", statistic.getPackageName());// 通过包名查询软件id询
		}
		List<PreinstallApk> preinstallApks = dao.query(PreinstallApk.class, cn2);
		ActivatingQuantity Quantity = new ActivatingQuantity();
		Cnd cnd = null;
		cnd = Where.where(cnd, "c_imei1", "=", updateClientInfo.getIMEI());// 通过唯一值查询手机
		List<Device> device = dao.query(Device.class, cnd);
		Cnd cnd1 = null;
		for (PreinstallApk preinstallApk : preinstallApks) {
			cnd1 = Where.where(cnd1, "q_apkId", "=", preinstallApk.getId());// 通过软件id查询软件
		}
		for (Device devices : device) {
			cnd1 = Where.where(cnd1, "q_deviceId", "=", devices.getId());// 通过手机id查询软件
		}
		//List<ActivatingQuantity> fetch = dao.query(ActivatingQuantity.class, cnd1);
		/*Timestamp tt = (Timestamp ) dao.func2("t_activating_quantity", "max", "q_createtime", cnd1);
		System.err.println("func记录"+tt);*/
		Sql sql = Sqls.create("select * from t_activating_quantity   where  q_createtime in (select max(q_createtime) from t_activating_quantity  $cnd )");
		sql.setVar("cnd1", Cnd.where("t_activating_quantity.q_apkId", "=", "q_apkId").and("t_activating_quantity.q_deviceId", "=", "q_deviceId"));
		sql.setCallback(Sqls.callback.entities());
		sql.setEntity(dao.getEntity(ActivatingQuantity.class));
		dao.execute(sql);
		List<ActivatingQuantity> fetch = sql.getList(ActivatingQuantity.class);
		System.err.println("----------list"+fetch.toString());
		if (fetch.isEmpty()) {
			for (Statistics statistics2 : statistics) {
				for (PreinstallApk preinstallApk : preinstallApks) {
					for (Device dev : device) {
						if (statistics2.getDate_Time().getTime() > preinstallApk.getUploadDateTime().getTime()) {
							if (num <= 4) {
							/*	num++;*/
								Quantity.setApkId(preinstallApk.getId()); // APK ID
								Quantity.setDeviceId(dev.getId()); // 手机 ID
								Quantity.setCreatetime(sdf.format(statistics2.getDate_Time())); // 更新时间
								Quantity.setTime(num + ""); // 次数
								Quantity = dao.insert(Quantity); // 插入数据
								System.out.println("-----------新增成功");
							}
						}
					}
				}
			}
		} else {
			// 判断手机唯一标示是否一致
			temp:for (Statistics statistics2 : statistics) {
				for (PreinstallApk preinstallApk : preinstallApks) {
					for (Device dev : device) {
						for (ActivatingQuantity activatingQuantity : fetch) {
							if (statistics2.getDate_Time().getTime() > preinstallApk.getUploadDateTime().getTime()) {
								if (num <= 4) {
									num++;
									System.err.println("第二次" + num);
									System.err.println("-----1--" + statistics2.getDate_Time());
									System.err.println("------2-" + sdf.parse(activatingQuantity.getCreatetime()));
									boolean isv = Util.isSameDate(statistics2.getDate_Time(),
											sdf.parse(activatingQuantity.getCreatetime()));
									System.err.println("---------" + isv);
									// 判断客户端发送的日期和数据库表的日期对比是否相同
									if (Util.isSameDate(statistics2.getDate_Time(),
											sdf.parse(activatingQuantity.getCreatetime()))) {
										break temp;
									}
									activatingQuantity.setApkId(preinstallApk.getId()); // APK ID
									activatingQuantity.setDeviceId(dev.getId()); // 手机 ID
									activatingQuantity.setCreatetime(sdf.format(statistics2.getDate_Time())); // 更新时间
									activatingQuantity.setTime(num + "");
									System.err.println("次数------"+activatingQuantity.getTime());
									activatingQuantity = dao.insert(activatingQuantity); // 插入数据
									System.out.println("-----------操作成功");
								}
							}
						}
					}
				}
			}
		}

		return Result.makeSuccessResult();

	}

日志如下:
----------list[ActivatingQuantity [id=1316, apkId=12, deviceId=109, createtime=2017-11-23 14:26:35.0, time=2], ActivatingQuantity [id=1317, apkId=12, deviceId=109, createtime=2017-11-23 14:26:35.0, time=2]]
第二次2
-----1--Thu Nov 23 14:26:35 CST 2017
------2-Thu Nov 23 14:26:19 CST 2017
---------false
次数------2
2017-11-23 14:29:23,586 org.nutz.dao.impl.sql.run.NutDaoExecutor.printSQL(NutDaoExecutor.java:388) DEBUG - INSERT INTO t_activating_quantity(q_apkId,q_deviceId,q_createtime,q_time) VALUES(?,?,?,?) 
    |  1 |   2 |                   3 | 4 |
    |----|-----|---------------------|---|
    | 12 | 109 | 2017-11-23 14:26:35 | 2 |
  For example:> "INSERT INTO t_activating_quantity(q_apkId,q_deviceId,q_createtime,q_time) VALUES(12,109,'2017-11-23 14:26:35','2') "
-----------操作成功
第二次3
-----1--Thu Nov 23 14:26:35 CST 2017
------2-Thu Nov 23 14:26:35 CST 2017
---------true
		for (PreinstallApk preinstallApk : preinstallApks) {
			cnd1 = Where.where(cnd1, "q_apkId", "=", preinstallApk.getId());// 通过软件id查询软件
		}
		for (Device devices : device) {
			cnd1 = Where.where(cnd1, "q_deviceId", "=", devices.getId());// 通过手机id查询软件
		}

这样的cnd1永远都是最后的device的值啊

问下 会跟第一个日期比较
-----1--Thu Nov 23 14:26:35 CST 2017   最新的日期
------2-Thu Nov 23 14:26:19 CST 2017  第一个的日期
我把项目的clean一下   svn clean  清除缓存  
没有上面的情况了
添加回复
请先登陆
回到顶部