NutzCN Logo
问答 框架复杂生成sql问题
发布于 2731天前 作者 呆呆的初行者 1603 次浏览 复制 上一个帖子 下一个帖子
标签:

我想插入一个group by 语句,插不进去。直接写在 $condition之前会报错,后面也会报错,因为 $condition包含一个where条件和一个order by 条件,必须要在他们中间使用group by 才能查出想要的数据,求解!!

SELECT unit.name,cloud_delivery_man.name as unit_name,cloud_delivery_man.sex,"
				+ "cloud_delivery_man.phone,cloud_delivery_man.delivery_man_id,a.delivery_id,a.order_id,
                                + "COUNT(a.delivery_man_id) AS orderAll,"
				+ "SUM(CASE WHEN order_status = '3' THEN 1 ELSE 0 END) AS orderFinish, "
				+ "SUM(CASE WHEN order_status = '4' THEN 1 ELSE 0 END) AS orderCancel, "
				+ "SUM(CASE WHEN order_status = '5' THEN 1 ELSE 0 END) AS orderError "
				+ "FROM sys_unit AS unit RIGHT JOIN cloud_delivery_man ON "
				+ "unit.id = cloud_delivery_man.delivery_id RIGHT JOIN order_delivery AS a ON "
				+ "cloud_delivery_man.delivery_man_id = a.delivery_man_id LEFT JOIN cloud_order b ON "
				+ "a.order_id = b.order_id $condition";



5 回复

目标SQL长什么样子???


if (orders != null && orders.size() > 0) { for (DataTableOrder order : orders) { cnd.orderBy("update_time", order.getDir()); } } map.put("recordsFiltered", this.dao().count(Cloud_order.class, cnd)); String sqlString = "SELECT d.id,d.name teamName ,e.nickname AS managerName,e.phone AS managerPhone,d.address AS teamAddress ," + " COUNT(a.delivery_man_id) AS orderAll," + "SUM(CASE WHEN b.order_status = '3' THEN 1 ELSE 0 END) AS orderFinish," + "SUM(CASE WHEN b.order_status = '4' THEN 1 ELSE 0 END) AS orderCancel," + "SUM(CASE WHEN b.order_status = '5' THEN 1 ELSE 0 END) AS orderError " + "FROM order_delivery a " + "INNER JOIN cloud_order b ON a.order_id=b.order_id " + "INNER JOIN cloud_delivery_man c ON a.delivery_id=c.delivery_id AND a.delivery_man_id=c.delivery_man_id " + "INNER JOIN sys_unit d ON a.delivery_id=d.id " + "INNER JOIN sys_user e ON d.id=e.unitid $condition GROUP BY a.delivery_id"; Sql sql = Sqls.create(sqlString); Entity<Record> entity = dao().getEntity(Record.class); sql.setEntity(entity); sql.setCondition(cnd); sql.setCallback(Sqls.callback.entities()); Pager pager = new OffsetPager(start, length); sql.setPager(pager); dao().execute(sql); 上面加了排序条件后 GROUP BY 语句变成了 在排序函数后面导致sql执行失败,group by只有在where语句后面,排序前面才能正常运作。 目标sql: SELECT d.id, d. NAME teamName, e.nickname AS managerName, e.phone AS managerPhone, d.address AS teamAddress, COUNT(a.delivery_man_id) AS orderAll, SUM(CASE WHEN b.order_status = '3' THEN 1 ELSE 0 END) AS orderFinish, SUM(CASE WHEN b.order_status = '4' THEN 1 ELSE 0 END) AS orderCancel, SUM(CASE WHEN b.order_status = '5' THEN 1 ELSE 0 END) AS orderError FROM order_delivery a INNER JOIN cloud_order b ON a.order_id = b.order_id INNER JOIN cloud_delivery_man c ON a.delivery_id = c.delivery_id AND a.delivery_man_id = c.delivery_man_id INNER JOIN sys_unit d ON a.delivery_id = d.id INNER JOIN sys_user e ON d.id = e.unitid WHERE e.nickname like %张% GROUP BY a.delivery_id ORDER BY update_time DESC LIMIT 0,10 实际sql: SELECT d.id, d. NAME teamName, e.nickname AS managerName, e.phone AS managerPhone, d.address AS teamAddress, COUNT(a.delivery_man_id) AS orderAll, SUM(CASE WHEN b.order_status = '3' THEN 1 ELSE 0 END) AS orderFinish, SUM(CASE WHEN b.order_status = '4' THEN 1 ELSE 0 END) AS orderCancel, SUM(CASE WHEN b.order_status = '5' THEN 1 ELSE 0 END) AS orderError FROM order_delivery a INNER JOIN cloud_order b ON a.order_id = b.order_id INNER JOIN cloud_delivery_man c ON a.delivery_id = c.delivery_id AND a.delivery_man_id = c.delivery_man_id INNER JOIN sys_unit d ON a.delivery_id = d.id INNER JOIN sys_user e ON d.id = e.unitid WHERE e.nickname like %张% ORDER BY update_time DESC LIMIT 0,10 GROUP BY a.delivery_id

cnd能写groupby的

给个例子,第一次用这个框架,cnd不是要给三个参数或者直接硬编码Cnd.wrap()么?硬编码我没有尝试出来,不知道是方法不对还是真的没用。

用的是很老的nutz版本?没看到Cnd类的groupby方法?

添加回复
请先登陆
回到顶部