如题,在使用groupby参数后,dao.count()得到的结果还是未使用groupby的情况
19 回复
给代码
来自炫酷的 NutzCN
Cnd cnd = Cnd.where("eventTime", "<", endTime).and("eventTime", ">", startTime);
if ("true2false".equals(change)) {
cnd.and("oldStatus", "=", true).and("newStatus", "=", false);
}else if ("false2true".equals(change)) {
cnd.and("oldStatus", "=", false).and("newStatus", "=", true);
}
if (Strings.isBlank(eventId)) {
cnd.groupBy("deviceId,eventId");
}else{
cnd.and("eventId", "=", eventId);
cnd.groupBy("deviceId");
}
if ("asc".equals(order)) {
cnd.asc("deviceId");
}else{
cnd.desc("deviceId");
}
int a = dao.count(DeviceStatusChangeInfo.class, cnd);
List<DeviceStatusChangeInfo> lst = dao.query(DeviceStatusChangeInfo.class, cnd);
int b = lst.size();
log.debug(a == b)
Cnd cnd = Cnd.where("eventTime", "<", endTime).and("eventTime", ">", startTime);
if ("true2false".equals(change)) {
cnd.and("oldStatus", "=", true).and("newStatus", "=", false);
}else if ("false2true".equals(change)) {
cnd.and("oldStatus", "=", false).and("newStatus", "=", true);
}
if (Strings.isBlank(eventId)) {
cnd.groupBy("deviceId,eventId");
}else{
cnd.and("eventId", "=", eventId);
cnd.groupBy("deviceId");
}
if ("asc".equals(order)) {
cnd.asc("deviceId");
}else{
cnd.desc("deviceId");
}
int a = dao.count(DeviceStatusChangeInfo.class, cnd);
List<DeviceStatusChangeInfo> lst = dao.query(DeviceStatusChangeInfo.class, cnd);
int b = lst.size();
log.debug(a == b)
@wendal 这个应该很容易复现,只要groupby的字段存在重复,就会出现计数错误
@wendal 用cnd.toSql()方法取得SQL么?方法里面的entity是填啥?
@luoyy 日志里面那条SQL
@wendal 看到了,生成的SQL出问题了
ECLIPSE上看到的cnd内容
WHERE eventTime<1470023231047 AND eventTime>1262275200000 GROUP BY deviceId,eventId ORDER BY deviceId ASC
控制台输出日志
2016-08-01 11:51:21,526 org.nutz.dao.impl.sql.run.NutDaoExecutor._runSelect(NutDaoExecutor.java:211) DEBUG - SELECT COUNT(*) FROM t_dsci WHERE eventTime<? AND eventTime>?
@wendal 可以看到count()把where之后的参数都忽略了
把最后一个排序注释一下试试
来自炫酷的 NutzCN
@wendal 还是一样,实际执行的语句只有where条件,groupby不见了
@wendal 好的,我现在准备转一下先用着,用Daos.queryCount(),所以要用cnd.toSql(),参数entity怎么填?
我的测试代码如下
@Test
public void test_groupby() throws MalformedURLException {
Cnd cnd = Cnd.where("id", ">", 1);
cnd.groupBy("name");
cnd.asc("name");
System.out.println(cnd);
dao.query(User.class, cnd);
}
输出
WHERE id>1 GROUP BY name ORDER BY name ASC
13:43:17.999 DEBUG (NutDaoExecutor.java:211) _runSelect - SELECT * FROM t_user WHERE id>? GROUP BY name ORDER BY name ASC
| 1 |
|---|
| 1 |
For example:> "SELECT * FROM t_user WHERE id>1 GROUP BY name ORDER BY name ASC "
带排序和不带排序均ok, 带不带System.out.println也ok.
刚试了一下, count会不行
13:47:10.998 DEBUG (NutDaoExecutor.java:211) _runSelect - SELECT COUNT(*) FROM t_user WHERE id>?
| 1 |
|---|
| 1 |
For example:> "SELECT COUNT(*) FROM t_user WHERE id>1"
@wendal 好的,那我先用自定义SQL顶着了
添加回复
请先登陆