当前位置: 首页 > 图文教程 > 数据库 > MYSQL > 扩展求方差的mysql函数例子

MYSQL
数据库新手入门之MySQL中如何定义外键
MySQL ODBC进行MySQL和SQL Server转换
三方法优化MySQL数据库查询
意料外的MySQL运算符可获更多数据功能
Sql Server 2005 数据库维护计划
搞定MySQL数据库中文模糊检索问题
MySQL服务器内部安全数据目录如何访问
SECURITY INVOKER存储过程权限提升漏洞
MySQL常见错误提示及解决方法
MySQL 5 C API 访问数据库例子程序
管理员必读10个重要MySQL客户启动选项
通过rpm包安装、配置及卸载mysql
实现MySQL数据库数据的同步方法介绍
六大步保护MySQL数据库中重要数据
MySQL数据库的内部以及外部安全性简介
MySQL数据库的数据备份与恢复学习
Java连接各种数据库的实例
UNIX设置MySql数据同步 实现复制功能
扩展求方差的mysql函数例子
MySQL 事务预编译查询和Perl DBI简化

MYSQL 中的 扩展求方差的mysql函数例子


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-17   浏览: 49 ::
收藏到网摘: n/a

源码

以下为引用的内容:

// Author: JiangMiao
// Name: 扩展求方差的MySQL函数列子
// Date: 2006-10-19
// Link: http://blog.sina.com.cn/u/1259926384 - JiangMiao的Blog
#include "winsock2.h"
#include "MySQL.h"
#include <vector>
using namespace std;
#define SAFE_DELETE(p) if(p!=NULL){delete p;p=NULL;}
#define CDLLEXPORT extern "C" __declspec(dllexport)
typedef __int64 longlong;
typedef vector<double> vec_double;
typedef unsigned long ulong;
class VAR
{
private:
  vec_double datas;
  double total;
public:
  VAR():total(0.0) {}
  //加入num
  void push_back(double num)
   {
   datas.push_back(num);
   total+=num;
   }
  void clear()
   {
   datas.clear();
   total=0.0;
   }
  //取方差
  double getVariance()
   {
   size_t count=datas.size();
   double avr=0.0;
   avr=(total/count); //平均数
   double rt=0.0;
   for(size_t i=0;i<count;i++)
    {
    double k=(datas[i]-avr);
    rt+=k*k;
    }
   return rt/count;
   }

};
CDLLEXPORT my_bool variance_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
{
initid->ptr = NULL;
if(args->arg_count!=1) //参数个数为1
  {
  return 1;
  }
if(args->arg_type[0]!=REAL_RESULT||args->arg_type[0]!=INT_RESULT) //参数类别为整型或double
  {
  return 1;
  }
initid->ptr = (char*)new VAR();
return 0;
}
CDLLEXPORT void variance_deinit(UDF_INIT *initid)
{
VAR* ptr=(VAR*)initid->ptr;
delete ptr;
}
CDLLEXPORT double variance(UDF_INIT *initid, UDF_ARGS *args,char *is_null, char *error)
{
VAR* ptr=(VAR*)initid->ptr;
return ptr->getVariance();
}
CDLLEXPORT void variance_clear(UDF_INIT *initid, char *is_null, char *error)
{
VAR* ptr=(VAR*)initid->ptr;
ptr->clear();
}
CDLLEXPORT void variance_add(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error)
{
VAR* ptr=(VAR*)initid->ptr;
char* argo=args->args[0];
double arg;
if(args->arg_type[0]==REAL_RESULT)
  {
  arg=*(double*)argo;
  }
if(args->arg_type[0]==INT_RESULT)
  {
  arg=(double)*(__int64*)argo;
  }
ptr->push_back(arg);
}

编译后得到variance.dll

复制到bin目录下

测试

以下为引用的内容:

MySQL> use test;
Database changed
MySQL> create table vartest (realtest real,inttest int);
Query OK, 0 rows affected (0.11 sec)

MySQL> insert into vartest values(5,5),(6,6),(9,9),(10,10),(5,5);
Query OK, 5 rows affected (0.03 sec)
Records: 5  Duplicates: 0  Warnings: 0

MySQL> create aggregate function variance returns real soname 'variance.dll';
Query OK, 0 rows affected (0.00 sec)

MySQL> select variance(realtest),variance(inttest) from vartest;
+--------------------+-------------------+
| variance(realtest) | variance(inttest) |
+--------------------+-------------------+
|                4.4 |            4.4000 |
+--------------------+-------------------+
1 row in set (0.00 sec)

MySQL>