1,select curdate()  /*2016-10-08*/

2,select date_sub(curdate(), INTERVAL 6 DAY) /*2016-10-02*/

3,case when then else 拼接条件查询:

WHERE
				(CASE  WHEN IFNULL(@belongCity,'0')='0' OR @belongCity=''  THEN  1 ELSE   

        CAST(LOCATE(@belongCity,worker_order.belong_city) AS SIGNED )   

        END)=1
AND

	(CASE  WHEN IFNULL(@energyStation,'0')='0'  THEN  1 ELSE   

        CAST(LOCATE(@energyStation,worker_order.repository_id) AS SIGNED )   

        END)=1
AND
	(CASE  WHEN IFNULL(@serviceStatioin,'0')='0'  THEN  1 ELSE   

        CAST(LOCATE(@serviceStatioin,worker_order.station_id) AS SIGNED )   

        END)=1
AND
				DATE_FORMAT(worker_order.create_time, '%Y-%m-%d') BETWEEN DATE_FORMAT(cast(@beginDateTime as datetime), '%Y-%m-%d') and DATE_FORMAT(cast(@endDateTime as datetime), '%Y-%m-%d')

GROUP BY DATE_FORMAT(worker_order.create_time,'%Y-%m-%d');

或者是这种:

WHERE
		LOCATE((CASE  WHEN IFNULL(@belongCity,'0')='0' OR @belongCity=''  THEN worker_order.belong_city ELSE  @belongCity END),worker_order.belong_city)>0
AND
	  LOCATE((CASE  WHEN IFNULL(@energyStation,'0')='0'  THEN  worker_order.repository_id ELSE   @energyStation END),worker_order.repository_id)>0
AND
	  LOCATE((CASE  WHEN IFNULL(@serviceStatioin,'0')='0'  THEN  worker_order.station_id ELSE  @serviceStatioin END),worker_order.station_id)>0
AND
		DATE_FORMAT(worker_order.create_time, '%Y-%m-%d') BETWEEN DATE_FORMAT(cast(@beginDateTime as datetime), '%Y-%m-%d') and DATE_FORMAT(cast(@endDateTime as datetime), '%Y-%m-%d')

GROUP BY DATE_FORMAT(worker_order.create_time,'%Y-%m-%d');

4,拼接starttime和endtime:

set @beginDateTime=(
										CASE
										WHEN ISNULL(beginDateTime) THEN
											date_sub(curdate(), INTERVAL 6 DAY)
										ELSE
											DATE_FORMAT(beginDateTime,'%Y-%m-%d')
										END
									); /*起始时间*/
set @endDateTime=(
										CASE
										WHEN ISNULL(endDateTime) THEN
											curdate()
										ELSE
											DATE_FORMAT(endDateTime,'%Y-%m-%d')
										END
									); /*结束时间*/

5,创建存储过程及调用存储过程:

DROP PROCEDURE IF EXISTS Pro_query_work_order_report;

CREATE PROCEDURE Pro_query_work_order_report (
	IN cityCode varchar(32),/*城市code*/
	IN energyStation INT,/*能源站code*/
	IN serviceStatioin INT,/*网点code*/
	IN beginDateTime datetime,/*开始时间*/
	IN endDateTime datetime/*结束时间*/
)
BEGIN

SET @belongCity = cityCode;/*所属城市code*/
set @energyStation=energyStation; /*能源站code*/
set @serviceStatioin=serviceStatioin; /*站点code*/
set @beginDateTime=(
										CASE
										WHEN ISNULL(beginDateTime) THEN
											date_sub(curdate(), INTERVAL 6 DAY)
										ELSE
											DATE_FORMAT(beginDateTime,'%Y-%m-%d')
										END
									); /*起始时间*/
set @endDateTime=(
										CASE
										WHEN ISNULL(endDateTime) THEN
											curdate()
										ELSE
											DATE_FORMAT(endDateTime,'%Y-%m-%d')
										END
									); /*结束时间*/

select 

	DATE_FORMAT(worker_order.create_time,'%Y-%m-%d') as create_time,/*创建时间*/
	(	select count(0)
		from biz_work_order tmp
		where
		DATE_FORMAT(tmp.create_time,'%Y-%m-%d')=DATE_FORMAT(worker_order.create_time,'%Y-%m-%d')
		or (tmp.create_time<DATE_FORMAT(worker_order.create_time,'%Y-%m-%d') and tmp.`status` in (1,2,4))
	)as should_change_worker_order,/*应换工单*/
	(
		select count(0)
		from biz_work_order tmp
		where
			DATE_FORMAT(tmp.create_time,'%Y-%m-%d')=DATE_FORMAT(worker_order.create_time,'%Y-%m-%d')
	) as new_add_work_order,/*新增工单*/
	(
		select count(0)
		from biz_work_order tmp
		where DATE_FORMAT(tmp.create_time,'%Y-%m-%d')=DATE_FORMAT(worker_order.create_time,'%Y-%m-%d') and tmp.`status`=3
	)as today_finished_order,/*今天已完成工单*/
	(
		select count(0)
		from biz_work_order tmp
		where DATE_FORMAT(tmp.create_time,'%Y-%m-%d')=DATE_FORMAT(worker_order.create_time,'%Y-%m-%d') and tmp.`status` in(1,2,4)
	) as today_unfinished_order,/*今天未完成订单*/
		cast(
		(select count(0)
		from biz_work_order tmp
		where DATE_FORMAT(tmp.create_time,'%Y-%m-%d')=DATE_FORMAT(worker_order.create_time,'%Y-%m-%d') and tmp.`status`=3
		)
		/
		(	select count(0)
		from biz_work_order tmp
		where
		DATE_FORMAT(tmp.create_time,'%Y-%m-%d')=DATE_FORMAT(worker_order.create_time,'%Y-%m-%d')
		or (tmp.create_time<DATE_FORMAT(worker_order.create_time,'%Y-%m-%d') and tmp.`status` in (1,2,4))
		)
		as decimal(18, 2)
	) as success_percent /*成功率*/

from biz_work_order worker_order

WHERE
				(CASE  WHEN IFNULL(@belongCity,'0')='0' OR @belongCity=''  THEN  1 ELSE   

        CAST(LOCATE(@belongCity,worker_order.belong_city) AS SIGNED )   

        END)=1
AND

	(CASE  WHEN IFNULL(@energyStation,'0')='0'  THEN  1 ELSE   

        CAST(LOCATE(@energyStation,worker_order.repository_id) AS SIGNED )   

        END)=1
AND
	(CASE  WHEN IFNULL(@serviceStatioin,'0')='0'  THEN  1 ELSE   

        CAST(LOCATE(@serviceStatioin,worker_order.station_id) AS SIGNED )   

        END)=1
AND
	DATE_FORMAT(worker_order.create_time, '%Y-%m-%d') BETWEEN DATE_FORMAT(cast(@beginDateTime as datetime), '%Y-%m-%d') and DATE_FORMAT(cast(@endDateTime as datetime), '%Y-%m-%d')

GROUP BY DATE_FORMAT(worker_order.create_time,'%Y-%m-%d');
END;

6,调用:(mysql 使用call进行调用,且不支持默认参数;sql server 使用exec进行调用,支持默认参数,有默认值的参数可以不传)

call Pro_query_work_order_report('50010',NULL,270,date_sub(curdate(), INTERVAL 6 DAY),curdate())

发现mysql怎么数据量大了,还没有sql server好用。。。难道是我的幻觉么。。

MySQL 存储过程常用SQL语句收集的更多相关文章

  1. MySQL的常用SQL语句.md

    修改密码 这是常见的大家一般都要用的 首先     安装成功了打开cmd --> mysql -u root -p -->输入你的密码     修改mysql root用户密码    格式 ...

  2. MySQL存储过程动态SQL语句的生成

    用Mysql存储过程来完成动态SQL语句,使用存储过程有很好的执行效率: 现在有要求如下:根据输入的年份.国家.节假日类型查询一个节假日,我们可以使用一般的SQL语句嵌入到Java代码中,但是执行效率 ...

  3. Oracle系统表整理+常用SQL语句收集

    欢迎和大家交流技术相关问题: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://g ...

  4. Oracle系统表整理+常用SQL语句收集(转载)

    原文:https://www.cnblogs.com/jiangxinnju/p/5840420.html-- DBA/ALL/USER/V_$/GV_$/SESSION/INDEX开头的绝大部分都是 ...

  5. mysql整理-常用sql语句

    一.常用sql show variables like 'character_set_client';#查询字符集 show databases;#列出所有的服务器上的数据库alter create ...

  6. MySQL之常用SQL语句

    1) 分表之后统计数据的总量 SELECT (a0.total + a1.total + a2.total + a3.total + a4.total + a5.total + a6.total + ...

  7. php中一些常用的语句收集

    清空数据表   truncate 表名; http://blog.knowsky.com/234205.htm    常用的SQL语句实例 http://blog.csdn.net/vericlong ...

  8. MySQL常用SQL语句/函数/存储过程

    一句话总结 SELECT count(*) FROM user WHERE id>0 GROUP BY name HAVING count(*)>1 ORDER BY count(*)DE ...

  9. 剑指Offer——常用SQL语句、存储过程和函数

    剑指Offer--常用SQL语句.存储过程和函数 常用SQL语句 1.在MySQL数据库建立多对多的数据表关系 2.授权.取消授权 grant.revoke grant select, insert, ...

随机推荐

  1. 第二次作业——MathExamLv2

    MathExamLv2--林志松 211406285 李明康 211606314 一.预估与实际 PSP2.1 Personal Software Process Stages 预估耗时(分钟) 实际 ...

  2. spring mvc(4)处理模型数据

    处理模型数据 Spring MVC 提供了以下几种途径输出模型数据: – ModelAndView: 处理方法返回值类型为 ModelAndView时, 方法体即可通过该对象添加 模型数据 – Map ...

  3. C++备忘知识整理

    一.设置字体颜色 C++控制台程序运行时输出框默认的文字颜色是白色,所以我常称其输出框为黑白框.但是这个文字样式不是固定不变的,是可以改变颜色的字体的.方法有两种: 1.设置输出框的框体属性.在运行时 ...

  4. STM32F103片外运行代码分析

    STM32F103片外运行代码分析 STM32F103有三种启动方式: 1.从片内Flash启动: 2.从片内RAM启动: 3.从片内系统存储器启动,内嵌的自举程序,用于串口IAP. 无法直接在片外N ...

  5. EF Core 2.1 中的 Eager loading、Explicit loading和LazyLoading (转自MSDN)

    Entity Framework Core allows you to use the navigation properties in your model to load related enti ...

  6. Oracle恢复误删数据

    1.先查出被删除的时间点: select * from flashback_transaction_query where table_name='表名'; 2.根据时间点恢复数据: insert i ...

  7. 工具 | Axure基础操作 No.4

    昨天因为有事没有学习,很愧疚,今天赶紧补上.俗话说,"学如逆水行舟,不进则退"还是很有道理的. 1.设置页面内容格式 这里主要是在浏览器中的内容所出现的对齐格式,左对齐或者是居中对 ...

  8. 丑数(Ugly Numbers, UVa 136)

    丑数(Ugly Numbers, UVa 136) 题目描述 我们把只包含因子2.3和5的数称作丑数(Ugly Number).求按从小到大的顺序的第1500个丑数.例如6.8都是丑数,但14不是,因 ...

  9. java web多组件协作实现用户登录验证

    实现步骤: 1.创建用户登录提交界面 2.创建处理用户登录请求servlet组件Main 3.创建代表登录成功响应的servlet的组件LoginSuccess 4.创建代表登录失败响应的servle ...

  10. 复习宝典之SpringMVC

    查看更多宝典,请点击<金三银四,你的专属面试宝典> 第七章:SpringMVC MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(co ...