mysql函数且做ifelse逻辑计算

为什么使用函数:

1、函数允许标准组件式编程,提高了SQL语句的重用性、共享性和可移植性

2、函数可以被作为一种安全机制来利用。

3、函数能够实现较快的执行速度,能够减少网络流量(一次编译多次执行)。 

CREATE DEFINER=`root`@`%` FUNCTION `arbitrationFees`(a decimal) RETURNS decimal(65,2)
BEGIN
	DECLARE _k decimal(65,2) DEFAULT 0.00;
	DECLARE _v decimal(65,2) DEFAULT 0.00;
	DECLARE _total decimal(65,2) DEFAULT 0.00;
	DECLARE _interest_rate decimal(65,2) DEFAULT 0.7;
	DECLARE _op_rate decimal(65,3) DEFAULT 0.105;
	if a < 1000 THEN
		set _k = 49;
		set _v = 210;
	ELSEIF (a<50000 AND a>=1000) THEN
		set _k = (70 + (a - 1000) * 0.045) * _interest_rate;
    set _v = 210;
	ELSEIF (a<100000 AND a>=50000) THEN
		set _k = (2275 + (a - 50000) * 0.035) * _interest_rate;
    set _v = _k * 0.105;
	ELSEIF (a<200000 AND a>=100000) THEN
		set _k = (4025 + (a - 100000) * 0.025) * _interest_rate;
    set _v = _k * 0.105;
	ELSEIF (a<500000 AND a>=200000) THEN
		set _k = (6525 + (a - 200000) * 0.015) * _interest_rate;
    set _v = _k * 0.105;
	ELSEIF (a<1000000 AND a>=500000) THEN
		set _k = (11025 + (a - 500000) * 0.007) * _interest_rate;
    set _v = _k * 0.105;
	ELSEif	a>=1000000 THEN
		set _k = (14525 + (a - 1000000) * 0.004) * _interest_rate;
    set _v = _k * _op_rate;
	END IF;
	  set _total = convert((_k + _v),decimal(65,2));
		return  _total;
END

 

你可能感兴趣的:(mysql)