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. 用sql语句实现年龄分段统计

    SELECT CASE WHEN (age >= 10 AND age <= 20) THEN '10-20' WHEN (age >= 21 AND age <= 30) T ...

  2. Junit4所需jar包

    在eclipse中新建一个Junit类,运行时出现java.lang.NoClassdeffounderror:org/apache/commons/logging/LogFactory错误,原来是缺 ...

  3. bzoj2331 [SCOI2011]地板

    Description lxhgww的小名叫“小L”,这是因为他总是很喜欢L型的东西.小L家的客厅是一个的矩形,现在他想用L型的地板来铺满整个客厅,客厅里有些位置有柱子,不能铺地板.现在小L想知道,用 ...

  4. FBKVOController代码阅读

    功能:对kvo机制进行封装,简化使用,简化内存管理: 要素:观察者.被观察者.处理函数. 模式:注册表模式: 机制:对象创建.注册管理.内存管理.处理机制转换: 其它:注册去重: kvo的管理机制:

  5. vector size函数使用注意事项

    vector 的size函数返回vector大小,返回值类型为size_type,Member type size_type is an unsigned integral type,即无符号整数: ...

  6. C#使用DotNetZip对zip压缩包进行添加删除操作

    参考:http://stackoverflow.com/questions/9855155/how-can-i-delete-a-directory-in-a-zip-file-using-net D ...

  7. CVPR 2016 paper reading (2)

    1. Sketch me that shoe, Qian Yu, Feng Liu, Yi-Zhe Song, Tao Xiang, Timothy M. Hospedales, Cheng Chan ...

  8. JAVA Collections常用方法

    /* 集合框架的工具类 Collections: */ import java.util.*; class Test { public static void main(String [] args) ...

  9. linux下安装使用tar

    安装tar,untar: yum install -y tar yum install -y untar 使用说明: 压缩 zip -r xxx.zip ./* 解压zip文件到当前目录 unzip ...

  10. python 基于udp 连接

    #udp 服务端 #导入socket模块 from socket import * #ip加端口 类型为元组 ip_port=('127.0.0.1',8002) #最大接收字节 buffer_siz ...