mysql 统计数据多条件写判断

  SELECT
      sum( CASE WHEN a.STATUS = 2 AND date_format( a.create_time, '%Y-%m-%d' ) = curdate() THEN 1 ELSE 0 END ) AS waitSendCount,
      sum( CASE WHEN a.order_type = 4 AND date_format( a.create_time, '%Y-%m-%d' ) = curdate() THEN 1 ELSE 0 END ) AS totalStay,
      sum( CASE WHEN date_format( a.create_time, '%Y-%m-%d' ) = curdate() THEN 1 ELSE 0 END ) AS total,
      sum( CASE WHEN a.STATUS = 2 AND date_format( a.create_time, '%Y-%m-%d' ) = date_sub(curdate(), interval 1 day) THEN 1 ELSE 0 END ) AS yesWaitSendCount,
      sum( CASE WHEN a.order_type = 4 AND date_format( a.create_time, '%Y-%m-%d' ) = date_sub(curdate(), interval 1 day) THEN 1 ELSE 0 END ) AS yesTotalStay,
      sum( CASE WHEN date_format( a.create_time, '%Y-%m-%d' ) = date_sub(curdate(), interval 1 day) THEN 1 ELSE 0 END ) AS yesTotal,
      b.school_name
      FROM ws_order a
	  LEFT JOIN ws_school b ON a.school_id = b.id
	  WHERE
	  a.is_deleted=0
	  AND a.teantId=#{teantId}
      GROUP BY a.school_id

你可能感兴趣的:(mysql,数据库)