NutzCN Logo
问答 自定义sql 少了参数判断
发布于 2335天前 作者 少年不再年少 2022 次浏览 复制 上一个帖子 下一个帖子
标签:
为什么sql 少了q_apkId=""的参数判断
for (PreinstallApk preinstallApk : preinstallApks) {
			//cnd1 = Where.where(cnd1, "q_apkId", "=", preinstallApk.getId());// 通过软件id查询软件
		sql.setVar("cnd", Cnd.NEW().and("q_apkId", "=", preinstallApk.getId()));
		//	sql.setVar("cnd", Where.where(cnd1, "q_apkId", "=", preinstallApk.getId()));
			System.err.println("=---"+ Cnd.NEW().and("q_apkId", "=", preinstallApk.getId()));
		}
		for (Device devices : device) {
			//cnd1 = Where.where(cnd1, "q_deviceId", "=", devices.getId());// 通过手机id查询软件
			//sql.setVar("cnd", Where.where(cnd1, "q_deviceId", "=", devices.getId()));
			
			sql.setVar("cnd", Cnd.NEW().and("q_deviceId", "=", devices.getId()));

			System.err.println("=---"+Cnd.NEW().and("q_deviceId", "=", devices.getId()));

		}
	//	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.NEW().andEX("t_activating_quantity.q_apkId", "=", "").andEX("t_activating_quantity.q_deviceId", "=", ""));
		sql.setCallback(Sqls.callback.entities());
		sql.setEntity(dao.getEntity(ActivatingQuantity.class));
	    Sql  sql1=  dao.execute(sql);
	    System.err.println("--------"+sql1.toString());
		List<ActivatingQuantity> fetch = sql.getList(ActivatingQuantity.class);
		System.err.println("--------"+fetch.toString());
日志
=--- WHERE q_apkId=1
=--- WHERE q_deviceId=112

--------select * from t_activating_quantity   where  q_createtime in (select max(q_createtime) from t_activating_quantity   WHERE q_deviceId=112 )
--------[ActivatingQuantity [id=1379, apkId=39, deviceId=112, createtime=2017-11-28 09:16:56.0, time=4]]
18 回复

你觉得sql.setVar会覆盖同名的键值对吗? 神一样的写法


我的写法有点问题 问下有其他的写法 for (PreinstallApk preinstallApk : preinstallApks) { //cnd1 = Where.where(cnd1, "q_apkId", "=", preinstallApk.getId());// 通过软件id查询软件 // sql.setVar("cnd", Cnd.NEW().and("q_apkId", "=", preinstallApk.getId())); sql.setVar("cnd", Where.where(cnd1, "q_apkId", "=", preinstallApk.getId())); System.err.println("=---"+ Cnd.NEW().and("q_apkId", "=", preinstallApk.getId())); } for (Device devices : device) { //cnd1 = Where.where(cnd1, "q_deviceId", "=", devices.getId());// 通过手机id查询软件 sql.setVar("cnd", Where.where(cnd1, "q_deviceId", "=", devices.getId())); // sql.setVar("cnd", Cnd.NEW().and("q_deviceId", "=", devices.getId())); System.err.println("=---"+Cnd.NEW().and("q_deviceId", "=", devices.getId())); } 日志如下 =--- WHERE q_apkId=1 =--- WHERE q_apkId=39 =--- WHERE q_deviceId=112
两条数据  才3个参数是错误的  
正确就是  WHERE q_apkId=1 WHERE  q_deviceId=112   
 WHERE q_apkId=39  WHERE q_deviceId=112

每调用一次 sql.setVar , 然后第一个参数写了"cnd", 那就只有最后一次生效啊, 这一点能不能理解呢???????

第一次  参数写cnd    第二次 参数写cnd 把一个的实例覆盖了  
	这样正确吗
Sql sql = Sqls.create("select * from t_activating_quantity   where  q_createtime in (select max(q_createtime) from t_activating_quantity  $cnd )");
		for (PreinstallApk preinstallApk : preinstallApks) {
			//cnd1 = Where.where(cnd1, "q_apkId", "=", preinstallApk.getId());// 通过软件id查询软件
		sql.setVar("cnd", Cnd.NEW().and("q_apkId", "=", preinstallApk.getId()));
	      	//sql.setVar("cnd", Where.where(cnd1, "q_apkId", "=", preinstallApk.getId()));
			System.err.println("=---"+ Cnd.NEW().and("q_apkId", "=", preinstallApk.getId()));
		}
		for (Device devices : device) {
			//cnd1 = Where.where(cnd1, "q_deviceId", "=", devices.getId());// 通过手机id查询软件
			//sql.setVar("cnd", Where.where(cnd1, "q_deviceId", "=", devices.getId()));
			Cnd.NEW().and("q_deviceId", "=", devices.getId());

			System.err.println("=---"+Cnd.NEW().and("q_deviceId", "=", devices.getId()));

		}

那还能不能在for 循环里面sql.setVar呢?

我老早就写过一个给你

Sql sql = Sqls.create("select * from t_activating_quantity   where  q_createtime in (select max(q_createtime) from t_activating_quantity  $cnd )");
Cnd cnd = Cnd.NEW();
for (PreinstallApk preinstallApk : preinstallApks) {
    cnd.or(cnd1, "q_apkId", "=", preinstallApk.getId()));
}
for (Device devices : device) {
   cnd.or("q_deviceId", "=", devices.getId());
}
sql.setVar("cnd", cnd);
Sql sql = Sqls.create("select * from t_activating_quantity   where  q_createtime in (select max(q_createtime) from t_activating_quantity  $cnd )");
Cnd cnd = Cnd.NEW();
for (PreinstallApk preinstallApk : preinstallApks) {
    cnd.or("q_apkId", "=", preinstallApk.getId()); // 怎么有个cnd1乱入, 已删
}
for (Device devices : device) {
   cnd.or("q_deviceId", "=", devices.getId());
}
sql.setVar("cnd", cnd);
or方法不是(String name, String op, Object value)不是三个参数吗
1.r.63版本更新or()方法吗

看最新的回复里面代码.

我看错了,我没有刷新页面  
这样写获取不到最新日期的记录  都是第一次的记录
select * from t_activating_quantity   where  q_createtime in (select max(q_createtime) from t_activating_quantity   WHERE q_apkId=1 OR q_apkId=1 AND q_apkId=39 OR q_apkId=39 AND q_deviceId=112 OR q_deviceId=112 )
--------[ActivatingQuantity [id=2192, apkId=1, deviceId=112, createtime=2017-11-28 16:31:58.0, time=1], ActivatingQuantity [id=2193, apkId=39, deviceId=112, createtime=2017-11-28 16:31:58.0, time=1]]

你想得到什么sql?是不知道业务对应的sql怎么写呢,还是不知道怎么把sql用java代码写?

为什么 没有相对软件的记录 这么查询其他软件的记录
=---1 WHERE q_apkId=39 OR q_apkId=39 OR q_apkId=39
=--- WHERE q_apkId=39 OR q_apkId=39 OR q_apkId=39 OR q_deviceId=112 OR q_deviceId=112
2017-11-29 10:13:54,622 org.nutz.dao.impl.sql.run.NutDaoExecutor.printSQL(NutDaoExecutor.java:388) DEBUG - select * from t_activating_quantity   where  q_createtime in (select max(q_createtime) from t_activating_quantity   WHERE q_apkId=? OR q_apkId=? OR q_apkId=? OR q_deviceId=? OR q_deviceId=? )
    |  1 |  2 |  3 |   4 |   5 |
    |----|----|----|-----|-----|
    | 39 | 39 | 39 | 112 | 112 |
  For example:> "select * from t_activating_quantity   where  q_createtime in (select max(q_createtime) from t_activating_quantity   WHERE q_apkId=39 OR q_apkId=39 OR q_apkId=39 OR q_deviceId=112 OR q_deviceId=112 )"
--------select * from t_activating_quantity   where  q_createtime in (select max(q_createtime) from t_activating_quantity   WHERE q_apkId=39 OR q_apkId=39 OR q_apkId=39 OR q_deviceId=112 OR q_deviceId=112 )
--------[ActivatingQuantity [id=2410, apkId=1, deviceId=112, createtime=2017-11-28 17:59:37.0, time=2]]
添加回复
请先登陆
回到顶部