NutzCN Logo
问答 sql自定义 参数赋值问题
发布于 2582天前 作者 少年不再年少 3090 次浏览 复制 上一个帖子 下一个帖子
标签:
如何把 cnd的参数  写 sql.setVar()方法
代码如下 
	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查询软件
		}
		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", "=", "q_apkId").andEX("t_activating_quantity.q_deviceId", "=", "q_deviceId"));
		sql.setCallback(Sqls.callback.entities());
		sql.setEntity(dao.getEntity(ActivatingQuantity.class));
		
	Sql  sql1=dao.execute(sql);
		
		System.err.println("----"+sql1.toString());
日志
----select * from t_activating_quantity   where  q_createtime in (select max(q_createtime) from t_activating_quantity   )
----------list[ActivatingQuantity [id=1372, apkId=1, deviceId=109, createtime=2017-11-25 10:09:50.0, time=4]]
19 回复

$cnd

sql.setVar("Cnd"

大小写对不上

把 Cnd 换成 cnd       sql.setVar()的参数如何赋值

那是占位符, 要对应啊

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", "=", "q_apkId").andEX("t_activating_quantity.q_deviceId", "=", "q_deviceId"));

$cnd是占位符

cnd1的参数如何 sql.setVar()方法

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查询软件
		}
问下  如何  ---[ [PackageName=com.UCMobile, Date_Time=Tue Nov 28 09:20:06 CST 2017, CS_INT=2],  [PackageName=com.jingdong.app.mall, Date_Time=Tue Nov 28 09:18:20 CST 2017, CS_INT=9]] 截取 两个数据

什么叫 "截取 两个数据"

另外, 你的回复通篇都是用代码格式包起来的?

[ [PackageName=com.ss.android.ugc.live, Date_Time=Tue Nov 28 09:52:52 CST 2017, CS_INT=2]]

[ [PackageName=com.jingdong.app.mall, Date_Time=Tue Nov 28 09:52:52 CST 2017, CS_INT=2]]

啥? 要分2次查询, 还是咋样?

我发现‘’q_apkId‘字符串是错误
如何把cnd1的参数替换的参数t_activating_quantity.q_apkId='q_apkId' 

----select * from t_activating_quantity   where  q_createtime in (select max(q_createtime) from t_activating_quantity   WHERE t_activating_quantity.q_apkId='q_apkId' AND t_activating_quantity.q_deviceId='q_deviceId' )

sql.setVar("cnd", Cnd.NEW().andEX("t_activating_quantity.q_apkId", "=", preinstallApk.getId()).andEX("t_activating_quantity.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 )");
		for (PreinstallApk preinstallApk : preinstallApks) {
			//cnd1 = Where.where(cnd1, "q_apkId", "=", preinstallApk.getId());// 通过软件id查询软件
			sql.setVar("cnd", Cnd.NEW().andEX("t_activating_quantity.q_apkId", "=", preinstallApk.getId()));
		}
		for (Device devices : device) {
		//	cnd1 = Where.where(cnd1, "q_deviceId", "=", devices.getId());// 通过手机id查询软件
			sql.setVar("cnd", Cnd.NEW().andEX("t_activating_quantity.q_apkId", "=", 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));
	    dao.execute(sql);
		List<ActivatingQuantity> fetch = sql.getList(ActivatingQuantity.class);

要查什么内容? 是不是想得到这样的SQL

// 假设preinstallApks有2个记录, id分别是A和B
// 假设 devices有两个记录(你的代码里面是单复数乱来的), id分别是X和Y

select * from t_activating_quantity   where  q_createtime in (select max(q_createtime) from t_activating_quantity where (t_activating_quantity.q_apkId='A' or t_activating_quantity.q_apkId = 'B') and (t_activating_quantity.q_apkId = 'X' or t_activating_quantity.q_apkId = 'Y'))

我想查手机下相对软件包名的最大日期的值,有就返回数据  没有返回空

那你知道自己想要怎么样的SQL吗?

例如 我获取安卓端 获取的数据  包含手机的型号,品牌,手机唯一值,软件的包名  软件打开的日期   ,我想查询  手机 下的安卓端当前打开软件包名的最大日期的记录  没有就新增  有数据 跟数据库的日期比较  大于数据库的日期 新增     等于数据库的日期 break;

那你知道自己想要怎么样的SQL吗?

知道是知道,不知道就不知道,回答一下啊

我现在获取的数据库最大日期的记录 ,不是相对的软件的最大日期的记录
日志
[PreinstallApk [id=1, channelId=1, uploaderId=1, advertisersId=1, name=UC浏览器, packageName=com.UCMobile, versionCode=700, versionName=11.6.2.948, price=1.8, SHA1= , memo=null, enable=true, uploadDateTime=Fri Aug 18 01:13:08 CST 2017, order=0, uploaderName=null, advertisersName=null, channelIds=[], time=0, application=0, totalprice=0.0, keep=0.0]]
2017-11-28 11:28:01,158 org.nutz.dao.impl.sql.run.NutDaoExecutor.printSQL(NutDaoExecutor.java:388) DEBUG - SELECT * FROM t_device  WHERE c_imei1=?
    |               1 |
    |-----------------|
    | 863170032238450 |
  For example:> "SELECT * FROM t_device  WHERE c_imei1='863170032238450'"
2017-11-28 11:28:01,196 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_deviceId=? )
    |   1 |
    |-----|
    | 112 |
  For example:> "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]]
为什么sql 少了q_apkId=""的参数判断
for (PreinstallApk preinstallApk : preinstallApks) {
			//cnd1 = Where.where(cnd1, "q_apkId", "=", preinstallApk.getId());// 通过软件id查询软件
		sql.setVar("cnd", Cnd.NEW().andEX("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]]
添加回复
请先登陆
回到顶部