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