NutzCN Logo
问答 nutz cnd Cnd这么判断条件为空?
发布于 2555天前 作者 少年不再年少 3411 次浏览 复制 上一个帖子 下一个帖子
标签:
--[ [PackageName=com.qihoo.browser, Date_Time=Fri Nov 24 16:07:52 CST 2017, CS_INT=8],  [PackageName=com.UCMobile, Date_Time=Fri Nov 24 14:33:00 CST 2017, CS_INT=20]]
2017-11-24 16:11:18,501 org.nutz.dao.impl.sql.run.NutDaoExecutor.printSQL(NutDaoExecutor.java:388) DEBUG - SELECT * FROM t_preinstall_apk  WHERE c_package_name=? AND c_package_name=?
    |                 1 |            2 |
    |-------------------|--------------|
    | com.qihoo.browser | com.UCMobile |
  For example:> "SELECT * FROM t_preinstall_apk  WHERE c_package_name='com.qihoo.browser' AND c_package_name='com.UCMobile'"
---1[]
2017-11-24 16:11:18,528 org.nutz.dao.impl.sql.run.NutDaoExecutor.printSQL(NutDaoExecutor.java:388) DEBUG - SELECT * FROM t_device  WHERE c_imei1=?
    |               1 |
    |-----------------|
    | 865687020107973 |
  For example:> "SELECT * FROM t_device  WHERE c_imei1='865687020107973'"
---2[Device [id=109, channelId=1, brand=Xiaomi, model=MI 4C, IMEI1=865687020107973, IMEI2=null, MEID=null, IMSI=null, OS=android, version=6.0.1, number0=null, number1=null, network=null, MAC=null, source=null, pluginName=blackcrow, pluginVersion=1.0.0, pluginKey=null, registerDateTime=Fri Nov 24 16:08:48 CST 2017, firstUpdateDateTime=Tue Nov 21 09:52:37 CST 2017, lastUpdateDateTime=Fri Nov 24 16:08:48 CST 2017, firstIP=192.168.1.119, firstArea=null, lastIP=192.168.1.119, lastArea=null, totalPreinstallApk=0, targetPreinstallApk=0, matchCount=0, IMEIS=-865687020107973-, softwareCount=2, channelName=null, pluginChannelName=null]]
----------select * from t_activating_quantity   where  q_createtime in (select max(q_createtime) from t_activating_quantity   )
2017-11-24 16:11:18,555 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   )
----------list[ActivatingQuantity [id=1366, apkId=1, deviceId=109, createtime=2017-11-24 14:33:00.0, time=2]]

32 回复

有andEX和orEX

问下写到 andEx和orEX  for (Statistics statistic : statistics) {
			cn2 = Where.where(cn2, "c_package_name", "=", statistic.getPackageName());// 通过包名查询软件id询
		}
吗
就是  = 等于 替换  andEx和orEX  吗


还是把  andEX  替换到   	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.setVar("cnd1", Cnd.NEW().andEX("t_activating_quantity.q_apkId", "=", q_apkId).andEX("t_activating_quantity.q_deviceId", "=", q_deviceId));

为啥你写的条件里面q_apkId和q_deviceId是个字符串?

不是,数据库字段  q_apkId和q_deviceId都是bigint类型

你的java语句里面写的是字符串,那是参数的值

我觉得不是sql 语句的问题  
我觉得  cn2 WHERE c_package_name='com.qihoo.browser' AND c_package_name='com.UCMobile'的问题
日志如下:
---[ [PackageName=com.qihoo.browser, Date_Time=Fri Nov 24 16:07:52 CST 2017, CS_INT=8],  [PackageName=com.UCMobile, Date_Time=Fri Nov 24 14:33:00 CST 2017, CS_INT=20]]
cn2 WHERE c_package_name='com.qihoo.browser' AND c_package_name='com.UCMobile'
2017-11-24 16:52:02,700 org.nutz.dao.impl.sql.run.NutDaoExecutor.printSQL(NutDaoExecutor.java:388) DEBUG - SELECT * FROM t_preinstall_apk  WHERE c_package_name=? AND c_package_name=?
    |                 1 |            2 |
    |-------------------|--------------|
    | com.qihoo.browser | com.UCMobile |
  For example:> "SELECT * FROM t_preinstall_apk  WHERE c_package_name='com.qihoo.browser' AND c_package_name='com.UCMobile'"
---1[]
2017-11-24 16:52:02,727 org.nutz.dao.impl.sql.run.NutDaoExecutor.printSQL(NutDaoExecutor.java:388) DEBUG - SELECT * FROM t_device  WHERE c_imei1=?
    |               1 |
    |-----------------|
    | 865687020107973 |
  For example:> "SELECT * FROM t_device  WHERE c_imei1='865687020107973'"
---2[Device [id=109, channelId=1, brand=Xiaomi, model=MI 4C, IMEI1=865687020107973, IMEI2=null, MEID=null, IMSI=null, OS=android, version=6.0.1, number0=null, number1=null, network=null, MAC=null, source=null, pluginName=blackcrow, pluginVersion=1.0.0, pluginKey=null, registerDateTime=Fri Nov 24 16:08:48 CST 2017, firstUpdateDateTime=Tue Nov 21 09:52:37 CST 2017, lastUpdateDateTime=Fri Nov 24 16:08:48 CST 2017, firstIP=192.168.1.119, firstArea=null, lastIP=192.168.1.119, lastArea=null, totalPreinstallApk=0, targetPreinstallApk=0, matchCount=0, IMEIS=-865687020107973-, softwareCount=2, channelName=null, pluginChannelName=null]]
----------select * from t_activating_quantity   where  q_createtime in (select max(q_createtime) from t_activating_quantity   )
2017-11-24 16:52:02,756 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   )
----------list[ActivatingQuantity [id=1366, apkId=1, deviceId=109, createtime=2017-11-24 14:33:00.0, time=2]]

你期望等到什么SQL,实际得到什么SQL

当-[ [PackageName=com.qihoo.browser, Date_Time=Fri Nov 24 16:07:52 CST 2017, CS_INT=8] ]一个值  
---1[]是有值的

--[ [PackageName=com.qihoo.browser, Date_Time=Fri Nov 24 16:07:52 CST 2017, CS_INT=8],  [PackageName=com.UCMobile, Date_Time=Fri Nov 24 14:33:00 CST 2017, CS_INT=20]]  多个值就是  
---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询
		}
		System.err.println("---"+statistics.toString());
		System.err.println("cn2"+cn2.toString());
		List<PreinstallApk> preinstallApks = dao.query(PreinstallApk.class, cn2);
		System.err.println("---1"+preinstallApks.toString());
		ActivatingQuantity Quantity = new ActivatingQuantity();
		Cnd cnd = null;
		cnd = Where.where(cnd, "c_imei1", "=", updateClientInfo.getIMEI());// 通过唯一值查询手机
		List<Device> device = dao.query(Device.class, cnd);
		System.err.println("---2"+device.toString());
		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.NEW().andEX("t_activating_quantity.q_apkId", "=", "q_apkId").andEX("t_activating_quantity.q_deviceId", "=", "q_deviceId"));
		sql.setCallback(Sqls.callback.entities());
		System.err.println("----------"+sql.toString());
		sql.setEntity(dao.getEntity(ActivatingQuantity.class));
		dao.execute(sql);
		List<ActivatingQuantity> fetch = sql.getList(ActivatingQuantity.class);
		System.err.println("----------list"+fetch.toString());

你期望等到什么SQL,实际得到什么SQL

我如何 ---[ [PackageName=com.qihoo.browser, Date_Time=Fri Nov 24 16:07:52 CST 2017, CS_INT=8],  [PackageName=com.UCMobile, Date_Time=Fri Nov 24 14:33:00 CST 2017, CS_INT=20]]  多个值的时候
---1[]也可以获取值的

单个值就是获取到值了
---[ [PackageName=com.qihoo.browser, Date_Time=Fri Nov 24 17:03:19 CST 2017, CS_INT=10]]  
cn2 WHERE c_package_name='com.qihoo.browser'
2017-11-24 17:03:26,109 org.nutz.dao.impl.sql.run.NutDaoExecutor.printSQL(NutDaoExecutor.java:388) DEBUG - SELECT * FROM t_preinstall_apk  WHERE c_package_name=?
    |                 1 |
    |-------------------|
    | com.qihoo.browser |
  For example:> "SELECT * FROM t_preinstall_apk  WHERE c_package_name='com.qihoo.browser'"
---1[PreinstallApk [id=5, channelId=0, uploaderId=1, advertisersId=2, name=360浏览器, packageName=com.qihoo.browser, versionCode=712, versionName=6.9.9.98beta, price=2.5, SHA1=eb1cd961e2e7835b9705e36c7b71f56dda37a054, memo=null, enable=true, uploadDateTime=Tue Aug 22 06:49:56 CST 2017, order=0, uploaderName=null, advertisersName=null, channelIds=[], time=0, application=0, totalprice=0.0, keep=0.0]]
2017-11-24 17:03:26,145 org.nutz.dao.impl.sql.run.NutDaoExecutor.printSQL(NutDaoExecutor.java:388) DEBUG - SELECT * FROM t_device  WHERE c_imei1=?
    |               1 |
    |-----------------|
		List<Statistics> statistics = updateClientInfo.getUserDate();
		Cnd cn2 = Cnd.NEW();
		for (Statistics statistic : statistics) {
			cnd2.orEX("c_package_name", "=", statistic.getPackageName());// 通过包名查询软件id,如果包名是null或者空字符串,跳过之
		}
		List<PreinstallApk> preinstallApks = dao.query(PreinstallApk.class, cn2); 

System.err.println("----------list"+fetch.toString());不是输出两条数据吗 为什么输出一条数据


---[ [PackageName=com.qihoo.browser, Date_Time=Fri Nov 24 17:12:15 CST 2017, CS_INT=11],  [PackageName=com.news.yazhidao, Date_Time=Fri Nov 24 17:18:04 CST 2017, CS_INT=16]]
cn2 WHERE c_package_name='com.qihoo.browser' OR c_package_name='com.news.yazhidao'
2017-11-24 17:22:30,858 org.nutz.dao.impl.sql.run.NutDaoExecutor.printSQL(NutDaoExecutor.java:388) DEBUG - SELECT * FROM t_preinstall_apk  WHERE c_package_name=? OR c_package_name=?
    |                 1 |                 2 |
    |-------------------|-------------------|
    | com.qihoo.browser | com.news.yazhidao |
  For example:> "SELECT * FROM t_preinstall_apk  WHERE c_package_name='com.qihoo.browser' OR c_package_name='com.news.yazhidao'"
---1[PreinstallApk [id=5, channelId=0, uploaderId=1, advertisersId=2, name=360浏览器, packageName=com.qihoo.browser, versionCode=712, versionName=6.9.9.98beta, price=2.5, SHA1=eb1cd961e2e7835b9705e36c7b71f56dda37a054, memo=null, enable=true, uploadDateTime=Tue Aug 22 06:49:56 CST 2017, order=0, uploaderName=null, advertisersName=null, channelIds=[], time=0, application=0, totalprice=0.0, keep=0.0], PreinstallApk [id=26, channelId=0, uploaderId=1, advertisersId=2, name=奇点资讯, packageName=com.news.yazhidao, versionCode=400002, versionName=4.0.2, price=1.3, SHA1=b4b9324628d31a423e713d5d9fd58b5256c53b2d, memo=null, enable=true, uploadDateTime=Tue Aug 22 10:44:49 CST 2017, order=0, uploaderName=null, advertisersName=null, channelIds=[], time=0, application=0, totalprice=0.0, keep=0.0]]
2017-11-24 17:22:30,900 org.nutz.dao.impl.sql.run.NutDaoExecutor.printSQL(NutDaoExecutor.java:388) DEBUG - SELECT * FROM t_device  WHERE c_imei1=?
    |               1 |
    |-----------------|
    | 865687020107973 |
  For example:> "SELECT * FROM t_device  WHERE c_imei1='865687020107973'"
---2[Device [id=109, channelId=1, brand=Xiaomi, model=MI 4C, IMEI1=865687020107973, IMEI2=null, MEID=null, IMSI=null, OS=android, version=6.0.1, number0=null, number1=null, network=null, MAC=null, source=null, pluginName=blackcrow, pluginVersion=1.0.0, pluginKey=null, registerDateTime=Fri Nov 24 17:08:52 CST 2017, firstUpdateDateTime=Tue Nov 21 09:52:37 CST 2017, lastUpdateDateTime=Fri Nov 24 17:08:52 CST 2017, firstIP=192.168.1.119, firstArea=null, lastIP=192.168.1.119, lastArea=null, totalPreinstallApk=0, targetPreinstallApk=0, matchCount=0, IMEIS=-865687020107973-, softwareCount=1, channelName=null, pluginChannelName=null]]
----------select * from t_activating_quantity   where  q_createtime in (select max(q_createtime) from t_activating_quantity   )
2017-11-24 17:22:30,929 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   )
----------list[ActivatingQuantity [id=1368, apkId=5, deviceId=109, createtime=2017-11-24 17:04:19.0, time=4]]

我不知道你的业务是怎么样的,我也不知道你数据库里面的数据是怎样的,我不能透过屏幕看到你的屏幕!!!

so, 我只能让你得到 "期望得到的SQL"


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.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)); System.err.println("----"+sql.toString());哪有错误 日志如下 ----select * from t_activating_quantity where q_createtime in (select max(q_createtime) from t_activating_quantity )
t_activating_quantity  $cnd )");
sql.setVar("cnd1"

$cnd 对不上 "cnd1"

$cnd 对不上 "cnd1"  还有数据输出

还有数据输出是啥意思? 你的SQL语句的占位符写的是"$cnd", 但你在调用setVar的时候传的名字是 "cnd1", 要改成"cnd"

可以执行一个数据判断  再执行第二个数据的判断   如何一条数据的执行判断的思路
如下是两个数据一起判断的
----------list[ActivatingQuantity [id=1372, apkId=1, deviceId=109, createtime=2017-11-25 10:09:50.0, time=4]]
2017-11-25 11:29:19,166 org.nutz.mvc.impl.UrlMappingImpl.get(UrlMappingImpl.java:101) DEBUG - Found mapping for [POST] path=/api/advert/quantity : PluginDataModule.ClientInfo(PluginDataModule.java:177)
2017-11-25 11:29:19,167 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) DEBUG - Get 'pluginDataModule'<class com.woyaofa.module.api.PluginDataModule>
------[ [PackageName=com.storm.smart, Date_Time=Sat Nov 25 10:50:23 CST 2017, CS_INT=7],  [PackageName=com.UCMobile, Date_Time=Sat Nov 25 11:21:25 CST 2017, CS_INT=40]]
---- [PackageName=com.storm.smart, Date_Time=Sat Nov 25 10:50:23 CST 2017, CS_INT=7]
---- [PackageName=com.UCMobile, Date_Time=Sat Nov 25 11:21:25 CST 2017, CS_INT=40]
2017-11-25 11:29:19,207 org.nutz.dao.impl.sql.run.NutDaoExecutor.printSQL(NutDaoExecutor.java:388) DEBUG - SELECT * FROM t_preinstall_apk  WHERE c_package_name=? OR c_package_name=?
    |               1 |            2 |
    |-----------------|--------------|
    | com.storm.smart | com.UCMobile |
  For example:> "SELECT * FROM t_preinstall_apk  WHERE c_package_name='com.storm.smart' OR c_package_name='com.UCMobile'"
---[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], PreinstallApk [id=12, channelId=0, uploaderId=1, advertisersId=2, name=暴风影音, packageName=com.storm.smart, versionCode=720300, versionName=7.2.03, price=1.8, SHA1=fdf60049c5a93f004ae06dc6fc7c40c76265bd8c, memo=null, enable=true, uploadDateTime=Tue Aug 22 06:57:20 CST 2017, order=0, uploaderName=null, advertisersName=null, channelIds=[], time=0, application=0, totalprice=0.0, keep=0.0]]
2017-11-25 11:29:19,248 org.nutz.dao.impl.sql.run.NutDaoExecutor.printSQL(NutDaoExecutor.java:388) DEBUG - SELECT * FROM t_device  WHERE c_imei1=?
    |               1 |
    |-----------------|
    | 865687020107973 |
  For example:> "SELECT * FROM t_device  WHERE c_imei1='865687020107973'"
2017-11-25 11:29:19,282 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   )
----------list[ActivatingQuantity [id=1372, apkId=1, deviceId=109, createtime=2017-11-25 10:09:50.0, time=4]]

这输出的sql就没变化,你到底改了没,这就是你想要的sql?

select * from t_activating_quantity   where  q_createtime in (select max(q_createtime) from t_activating_quantity   )
第一次判断是正确的  第二次判断 是错的  如何第二次判断就一个条件
111----{"Date_Time":"2017-11-25 14:54:55","CS_INT":4,"PackageName":"com.UCMobile"}
cn WHERE c_package_name='com.UCMobile'
111----{"Date_Time":"2017-11-25 15:07:49","CS_INT":16,"PackageName":"com.jingdong.app.mall"}
cn WHERE c_package_name='com.UCMobile' OR c_package_name='com.jingdong.app.mall'
如何第二次判断就一个条件

这是啥意思? cnd对象可不能删条件,只能加条件

如何  实现如下
111----{"Date_Time":"2017-11-25 14:54:55","CS_INT":4,"PackageName":"com.UCMobile"}
cn WHERE c_package_name='com.UCMobile'
111----{"Date_Time":"2017-11-25 15:07:49","CS_INT":16,"PackageName":"com.jingdong.app.mall"}
cn WHERE c_package_name='com.jingdong.app.mall'
	 JSONObject jArray = jsonArray.getJSONObject(i);
	    		System.err.println("111----"+jArray.toString());
	    		 jArray.optString("Date_Time");
	    		 jArray.optString("CS_INT");
	    		 jArray.optString("PackageName");
	    		 cn2.orEX("c_package_name", "=", jArray.optString("PackageName"));
	    		// cn2 = Where.where(cn2, "c_package_name", "=",  jArray.optString("PackageName"));	
	    		
	    		 System.err.println("cn"+cn2);
日志如下:
	111----{"Date_Time":"2017-11-25 14:54:55","CS_INT":4,"PackageName":"com.UCMobile"}
cn WHERE c_package_name='com.UCMobile'
111----{"Date_Time":"2017-11-25 15:07:49","CS_INT":16,"PackageName":"com.jingdong.app.mall"}
cn WHERE c_package_name='com.UCMobile' OR c_package_name='com.jingdong.app.mall'

用不同cnd实例

第一次查的时候建个cnd对象,二次查的时间再新建一个cnd对象,不要混着用cnd对象

多个数据 不就是多个cnd  实例   就是一一对应
如果40个数据  不是40个实例  
我知道数据不是固定会变的  Cnd对象可以实时创建吗

如果你有40个实例,那应该用where xxx in (xx,yy,zz)一次查出

List<User> users = dao.query(User.class, Cnd.where("name", "in", names.toArray());

输出的sql

select * from t_user where name in("wendal", "zozoh")
问下  cn2.where(name, op, value) 方法不行
添加回复
请先登陆
回到顶部