似外卖,淘宝用mysql统计好评差评

有三张表商家表,订单表,评价表
一个商家多个订单,一个订单对应一个评价,订单表有商家id,评价表有商家id还有订单id
sql=" SELECT s.,(SELECT count(e.id) from estimate e WHERE s.id=e.restaurant_id and e.score>3) as good,"
+ “(SELECT count(e.id) from estimate e WHERE s.id=e.restaurant_id and e.score=3) as middle,”
+ “(SELECT count(e.id) from estimate e WHERE s.id=e.restaurant_id and e.score<3) as bad,”
+ “(SELECT count(o.id) from shop_food_order o WHERE s.id=o.sEmpCode) as orderNum,”
+ “(SELECT sum(e.score)/count(e.id) from estimate e WHERE s.id=e.restaurant_id) as avgScore,”
+ “(SELECT sum(o.totalPrice) from shop_food_order o WHERE s.id=o.sEmpCode) as priceSum "
+ “from shop s ORDER BY orderNum DESC”;
按周月年统计,以当前时间推一周一月或一年
String cycle=getPara(“cycle”);
Date date=new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(date);
Format f = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
if(“周”.equals(cycle)){
cal.add(Calendar.DAY_OF_MONTH, -7);
String a=f.format(cal.getTime());
System.out.print(a);
sql=” SELECT s.
,(SELECT count(e.id) from estimate e,shop_food_order o WHERE s.id=e.restaurant_id and o.id = e.order_id and e.score>3 and o.createDate > ‘"+a+"’) as good,"
+ “(SELECT count(e.id) from estimate e,shop_food_order o WHERE s.id=e.restaurant_id and e.score=3 and o.id = e.order_id and o.createDate > '”+a+"’) as middle,"
+ “(SELECT count(e.id) from estimate e,shop_food_order o WHERE s.id=e.restaurant_id and e.score<3 and o.id = e.order_id and o.createDate > '”+a+"’) as bad,"
+ “(SELECT count(o.id) from shop_food_order o WHERE s.id=o.sEmpCode and o.createDate > '”+a+"’) as orderNum,"
+ “(SELECT sum(o.totalPrice) from shop_food_order o WHERE s.id=o.sEmpCode and o.createDate > '”+a+"’) as priceSum,"
+ “(SELECT sum(e.score)/count(e.id) from estimate e ,shop_food_order o WHERE s.id=e.restaurant_id and o.id = e.order_id and o.createDate > '”+a+"’) as avgScore"
+ " from shop s ORDER BY orderNum DESC";
最后提醒一下,sql里日期是需要加引号的

猜你喜欢

转载自blog.csdn.net/weixin_43190879/article/details/85598553