Mysql Lua语言拼接
【1】Mysql Lua语言拼接实例
(1)基本语句
local sql = "select * from task_control where cycle_id = '" .. args["cycleid"] .. "' and state = 6;"
local sql = "update task_control set state = 7 where cycle_id = '" .. args["cycleid"] .. "';"
local sql = "select sum(if(cycle_low > 0, if(cycle_low > lastbalance, lastbalance, cycle_low), lastbalance)) 'gift' from cfg_acct_free_res " ..
" where product_id = '" .. args["productid"] .. "' and call_type & " .. args["calltype"] ..
" > 0 and disable_time > sysdate() and enable_time < sysdate();"
local sql = "select lastbalance 'cash' from cfg_subscriber_prepay " .. " where product_id = '" .. args["productid"] .. "' and " .. " disable_time > '" .. cycleid_str .. "01' and enable_time < '" .. getNextcyceid(cycleid_str) .. "01' " .. " order by id DESC LIMIT 1;"
local sql = "select fee_rate 'fee_rate' from cfg_fee_rate c where c.product_id = '" .. args["productid"] .."' and call_type & " .. args["calltype"] .. " > 0 and disable_date > sysdate() and enable_date < sysdate() order by level ASC limit 1;"
local sql = 'call sdrrating(\'' .. args["account"] .. '\', \'' .. args["cycleid"] .. '\');'
local sql = "select id, product_id, start_time, end_time, call_type from bill_user_special where " ..
" cycle_id = '" .. cycleid_str .. "' and (isnull(process_cycleid) or process_cycleid <> '" .. cycleid_str .. "') " ..
" order by product_id;"
local sql = "update view_dat_sdr_" .. cycleid_str .. " set service_qty = floor((duration + fee_unit - 1) / fee_unit), " ..
" service_fee = floor((duration + fee_unit - 1) / fee_unit) * fee_rate " ..
" where product_id = '" .. user["product_id"] .. "' and call_type = 1;"
(2)replace into 语句
local sql = "replace into dat_bill_" .. args["cycleid"] .. "(" ..
"custom_id," .. -- 2
"contract_id," .. -- 3
"product_id," .. -- 4
"call_type," .. -- 5
-- 总费用
"total_fee," .. -- 6
"total_duration," .. -- 7
"total_times," .. -- 8
-- 结算费用
-- 过程控制*/
") select '" ..
args["cycleid"] .. "'," .. -- 1
"''," .. -- 2
"''," .. -- 3
"product_id," .. -- 4
"call_type," .. -- 5
-- 总费用
"sum(service_fee)," .. -- 6
"sum(duration)," .. -- 7
"0," .. -- 9
"0," .. -- 10
-- 结算费用
"source," .. -- 11
"sum(duration)," .. -- 12
"count(*)," .. -- 13
"sum(np_fee)," .. -- 14
-- 过程控制
"'00'," .. -- 15
" from view_dat_sdr_" .. args["cycleid"] .. " where product_id = '" .. args["account"] .. "' group by call_type;"
(3)TRUNCATE TABLE 语句
local sql = "TRUNCATE TABLE cfg_subscriber_prepay;" .. " INSERT INTO cfg_subscriber_prepay SELECT * FROM cfg_subscriber_prepay_" .. args["cycleid"] .. ";" .. " DROP TABLE cfg_subscriber_prepay_" .. args["cycleid"] .. "; commit;"
local undoSql = "START TRANSACTION;" ..
" REPLACE INTO cfg_subscriber_prepay " ..
" SELECT * FROM cfg_subscriber_prepay_" .. args["cycleid"] .. " WHERE product_id = '" .. args["productid"] .. "';" ..
" DELETE FROM cfg_subscriber_prepay_" .. args["cycleid"] .. " WHERE product_id = '" .. args["productid"] .. "';" ..
" REPLACE INTO cfg_acct_free_res " ..
" SELECT * FROM acct_free_res_" .. args["cycleid"] .. " WHERE product_id = '" .. args["productid"] .. "';" ..
" DELETE FROM acct_free_res_" .. args["cycleid"] .. " WHERE product_id = '" .. args["productid"] .. "';" ..
" DELETE FROM cfg_prepay_history WHERE product_id = '" .. args["productid"] .. "' AND cycle_id = '" .. args["cycleid"] .. "';" ..
" DELETE FROM dat_bill_" .. args["cycleid"] ..
" WHERE cycle_id = '" .. args["cycleid"] .. "' AND product_id = '" .. args["productid"] .. "' AND call_type = '1048576';" ..
" COMMIT;"
(4)SUM
local np_sum_fee = "SELECT product_id, IFNULL(SUM(duration), 0) AS np_sum_duration, COUNT(id) AS np_sum_cdrCnt, " .. " IFNULL(SUM(service_qty), 0) AS np_service_qty, IFNULL(SUM(service_fee), 0) AS np_service_fee, " .. " IFNULL(SUM(np_fee), 0) AS np_sum_fee, IFNULL(SUM(original_fee), 0) AS np_sum_original_fee " .. " FROM np_cdr_" .. cycleid .. " WHERE product_id = '" .. record["product_id"] .. "';"
(5)整体过程
local select_productid = "select product_id from np_cdr_" .. cycleid .. " group by product_id;"
local rtn, productid_set = executeSql(select_productid)
if rtn then
local sql_const = "insert into compare_fee_" .. cycleid .. " (product_id, np_id, np_sum_duration, np_total_times, " ..
"np_sum_qty, np_sum_fee, un_sum_duration, un_total_times, un_sum_fee, sum_np_fee, sum_original_fee) values "
if type(productid_set) == 'table' and next(productid_set) ~= nil then
for key, record in pairs(productid_set) do
local amount_table = {}
amount_table["product_id"] = record["product_id"]
amount_table["np_id"] = 'all'
-- 【1】统计
local np_sum_fee = "SELECT product_id, IFNULL(SUM(duration), 0) AS np_sum_duration, COUNT(id) AS np_sum_cdrCnt, " ..
" IFNULL(SUM(service_qty), 0) AS np_service_qty, IFNULL(SUM(service_fee), 0) AS np_service_fee, " ..
" IFNULL(SUM(np_fee), 0) AS np_sum_fee, IFNULL(SUM(original_fee), 0) AS np_sum_original_fee " ..
" FROM np_cdr_" .. cycleid .. " WHERE product_id = '" .. record["product_id"] .. "';"
local rtn, desc = executeSql(np_sum_fee)
if rtn and next(desc) ~= nil then
amount_table[]["np_sum_duration"]
amount_table[]["np_sum_cdrCnt"]
amount_table[]["np_service_qty"]
amount_table[]["np_service_fee"]
amount_table[]["np_sum_fee"]
amount_table[]["np_sum_original_fee"]
end
-- 【2】汇总
local un_sum_fee = "SELECT product_id, IFNULL(SUM(total_duration), 0) AS un_sum_duration, IFNULL(SUM(total_times), 0) AS un_sum_cdrCnt, " ..
" IFNULL(SUM(total_fee), 0) AS un_sum_fee FROM dat_bill_" .. cycleid .. " WHERE call_type & 4097 > 0 AND product_id = '" .. record["product_id"] .. "';"
local rtn, desc = executeSql(un_sum_fee)
if rtn and next(desc) ~= nil then
amount_table[]["un_sum_duration"]
amount_table[]["un_sum_cdrCnt"]
amount_table[]["un_sum_fee"]
end
-- 构建sql语句
then
insert_sql = insert_sql .. ","
else
insert_sql = sql_const
end
insert_sql = insert_sql .. "('" ..
amount_table["product_id"] .. "', '" ..
amount_table["np_id"] .. "', " ..
() .. ", " ..
() .. ", " ..
() .. ", " ..
() .. ", " ..
() .. ", " ..
().. ", " ..
() .. ", " ..
() .. ", " ..
() .. ")"
value_count = value_count +
== value_count then
sum_count = sum_count + value_count
insert_sql = insert_sql .. "; commit;"
local rtn, result = executeSql(insert_sql)
if not rtn then
rtnstr[
rtnstr["msg"] = "Error. amount_sum_fee insert into sum_fee num : " .. sum_count .. " sql : " .. insert_sql
return false
else
local result = "compare: success to amount_sum_fee insert into sum_fee num : " .. sum_count
ngx.log(ngx.ERR, result)
end
insert_sql = ""
value_count =
end
end
then
sum_count = sum_count + value_count
insert_sql = insert_sql .. "; commit;"
local rtn, result = executeSql(insert_sql)
if not rtn then
rtnstr[
rtnstr["msg"] = "Error. amount_sum_fee insert into sum_fee num : " .. sum_count .. " sql : " .. insert_sql
return false
else
local result = "compare: success to amount_sum_fee insert into sum_fee num : " .. sum_count
ngx.log(ngx.ERR, result)
end
insert_sql = ""
value_count =
end
end
(6)常量sql 拼接其他sql语句
local insert_const_sql = "insert into dat_bill_" .. cycleid_str .. "(" ..
" custom_id," .. -- 2
" contract_id," .. -- 3
" product_id," .. -- 4
" call_type," .. -- 5
-- [1]
" total_fee," .. -- 6
" total_duration," .. -- 7
" total_times," .. -- 8
-- [2]
" np_id," .. -- 11
" np_duration," .. -- 12
" np_times," .. -- 13
" np_fee," .. -- 14
-- [3]
local select_sql = "select " ..
"product_id, " .. -- 4
"call_type, " .. -- 5
-- [1]
"IFNULL(sum(service_fee), 0) as un_total_fee, " .. -- 6
"IFNULL(sum(duration), 0) as total_duration, " .. -- 7
"count(*) as total_times, " .. -- 8
-- [2]
"source, " .. -- 11
"IFNULL(sum(np_fee), 0) as np_total_fee, " .. -- 14 integer NULL,
-- [3]
" from view_dat_sdr_" .. cycleid_str .. " where product_id = '" .. productid .. "' and call_type & " .. calltype .. " > 0 " ..
" and (reserve_00 = '0' OR reserve_00 = '2') group by call_type;"
local rtn, desc = executeSql(select_sql, cfg.is_master_bill)
if not rtn then
rtnstr[
rtnstr["msg"] = "failed to genBill select data cycleid : " .. cycleid_str .. " productid : " .. productid .. " error : " .. desc
ngx.log(ngx.ERR, rtnstr["msg"])
else
if type(desc) == "table" and next(desc) ~= nil then
local insert_sql = ""
for key, value in ipairs(desc) do
insert_sql = insert_sql .. insert_const_sql
insert_sql = insert_sql .. "('" .. cycleid_str .. "', '', '', '" .. productid .. "', " .. value["call_type"] .. ", " ..
value["un_total_fee"] .. ", " .. value["total_duration"] .. ", " .. value["total_times"] .. ", 0, 0, '" ..
value["source"] .. "', " .. value["total_duration"] .. ", " .. value["total_times"] .. ", " .. value["np_total_fee"] .. ", '00', '" ..
value["current"] .. "', '" .. value["current"] .. "')" ..
" ON DUPLICATE KEY UPDATE" ..
" total_fee = " .. value["un_total_fee"] .. ", total_duration = " .. value["total_duration"] .. "," ..
" total_times = " .. value["total_times"] .. ", np_duration = " .. value["total_duration"] .. "," ..
" np_times = " .. value["total_times"] .. ", np_fee = " .. value["np_total_fee"] .. "," ..
" insert_time = '" .. value["current"] .. "', update_time = '" .. value["current"] .. "'; "
end
--ngx.log(ngx.ERR, "genBill insert into dat_bill_" .. cycleid_str .. " insert_sql : " .. insert_sql)
local rtn, err = executeSql(insert_sql)
if not rtn then
rtnstr[
rtnstr["msg"] = "Error. failed to genBill insert into dat_bill_" .. cycleid_str .. " productid : " .. productid ..
" call_type : " .. value["call_type"] .. " error : " .. err
ngx.log(ngx.ERR, rtnstr["msg"])
end
else
rtnstr[
rtnstr["msg"] = "OK. genBill bill productid : " .. productid .. " cycleid : " .. cycleid_str .. " is empty"
ngx.log(ngx.ERR, rtnstr["msg"])
end
end
(7)命令LOAD DATA INFILE
lcoal sql = "LOAD DATA INFILE '" .. csvdir .. string.gsub(offlinefile["filename"], ".zip", ".csv") .. "' " .. "INTO TABLE np_cdr_" .. cycleid_str .." FIELDS TERMINATED BY ',' IGNORE 1 LINES (product_id,calling_id,start_time,duration,call_type,@fee) " .. "set original_fee = @fee * 1000, insert_time = sysdate(), " .. "np_id = '" .. offlinefile["np_id"] .. "'," .. "cdr_state = '00', " .. "source = '" .. offlinefile["filename"] .. "';"
(8)IFNULL 和 FLOOR函数
local sql = "select call_type, ani_area, dnis_area, fee_rate, fee_unit, fee_type, transfer_area, " ..
" ifnull(np_id,'none') 'np_id', " ..
" ifnull(to_days(enable_date) - TO_DAYS('19700101') - 1, 0) 'startdate', " ..
" ifnull(to_days(disable_date) - TO_DAYS('19700101') + 1, 0) 'enddate'" ..
" From cfg_fee_rate where product_id = '" ..
productid .. "' and (fee_type = 'voice' or fee_type = 'sms') order by level DESC, enable_date DESC;"
local update_sql = "update np_cdr_" .. cycleid_str .. " set np_unit = IF(call_type = '长话', " .. long_rate["fee_unit"] .. ", " .. short_rate["fee_unit"] .. "), " .. " np_rate = IF(call_type = '长话', " .. long_rate["fee_rate"] .. ", " .. short_rate["fee_rate"] .. "), ".. " np_qty = FLOOR((duration + np_unit - 1) / np_unit), " .. " np_fee = FLOOR((duration + np_unit - 1) / np_unit) * np_rate " .. " where np_id = '" .. offlinefile["np_id"] .. "' and source = '" .. offlinefile["filename"] .. "';"
(9)distinct关键字
local sql = "select distinct a.id, a.product_id, a.realbalance, a.balance " ..
" from cfg_subscriber_prepay a " ..
" where a.status = '01' and a.disable_time > '" .. lastyearmonth .. "01' and " ..
" a.enable_time < '" .. (getNextcyceid(lastyearmonth)) .. "01' order by a.product_id, a.id;"
(10)常量值语句
local sqlstr = "replace into dat_bill_" .. cycleid_str .. "(cycle_id, custom_id, contract_id, product_id, call_type, total_fee," ..
" total_duration, total_times, discount_fee, cash_fee, np_id, np_duration, np_times, np_fee," ..
" bill_state, insert_time, update_time) select '" .. cycleid_str ..
"' , '+', '+', product_id, call_type, fee_rate" ..
", 1, 1, 0" ..
", 0, '0', 1, 1, 0, '0', sysdate(), sysdate() from cfg_fee_rate where call_type & ".. string.format("%d", cfg.globalcalltypecode['service']) .. " > 0 and disable_date >= '" ..
getNextcyceid(cycleid_str) .. "01' and enable_date < '" .. getNextcyceid(cycleid_str) .. "01' order by product_id;"
(11)ON DUPLICATE KEY UPDATE 语句
local sqlstr = "update cfg_acct_free_res set " .. computebalance .. " = " ..
string.format("%d", fr_record[refbalance] - fr_record["cycle_low"]) ..
" where id = " .. fr_record["id"] .. ";"
sqlstr = sqlstr .. "insert into dat_bill_" .. cycleid_str .. "(cycle_id, custom_id, contract_id, product_id, call_type, total_fee," ..
"total_duration, total_times, discount_fee, cash_fee, np_id, np_duration, np_times, np_fee," ..
"bill_state, insert_time, update_time) values('" .. -- values
cycleid_str ..
"', '+', '+', '" .. freeresproduct .."', 131072," ..
string.format("%d", fr_record["cycle_low"] - fr_record["resumesum"]) ..
", 1, 1," ..
string.format("%d", fr_record["cycle_low"] - fr_record["resumesum"]) ..
", 0, '0', 1, 1, 0, '0', sysdate(), sysdate()) on duplicate key update " .. -- on duplicate key update
"total_fee = total_fee + " .. string.format("%d", fr_record["cycle_low"] - fr_record["resumesum"]) .."," ..
"discount_fee = discount_fee + " .. string.format("%d", fr_record["cycle_low"] - fr_record["resumesum"]) .."," ..
"total_times = total_times + 1;"
Good Good Study, Day Day Up.
顺序 选择 循环 总结
Mysql Lua语言拼接的更多相关文章
- 【quick-cocos2d-x】Lua 语言基础
版权声明:本文为博主原创文章,转载请注明出处. 使用quick-x开发游戏有两年时间了,quick-x是cocos2d-Lua的一个豪华升级版的框架,使用Lua编程.相比于C++,lua的开发确实快速 ...
- Lua语言中文手册 转载自网络
Programming in LuaCopyright ® 2005, Translation Team, www.luachina.net Programming in LuaProgramming ...
- 用VC编译lua源码,生成lua语言的解释器和编译器
用VC编译lua源码,生成lua语言的解释器和编译器 1.去网址下载源码 http://www.lua.org/download.html 2.装一个VC++,我用的是VC6.0 3.接下来我们开始编 ...
- 51CTO专访淘宝清无:漫谈Nginx服务器与Lua语言
http://os.51cto.com/art/201112/307610.htm 说到Web服务器,也许你第一时间会想到Apache,也许你会想到Nginx.虽然说Apache依然是Web服务器的老 ...
- lua语言入门之Sublime Text设置lua的Build System
转自: http://blog.csdn.net/wangbin_jxust/article/details/8911956 最近开始学习LUA语言,使用Sublime Text作为编辑器,不得不说, ...
- 编译并使用Lua语言
Lua是一个小巧的脚本语言,该语言设计的目的是为了嵌入应用程序中,从而为应用程序提供灵活的扩展和定制功能. 可扩展性.Lua的扩展性非常卓越,可以通过Lua代码或C代码扩展,很多功能可以通过外部库来扩 ...
- Lua语言在Wireshark中使用(转)
1. 检查Wireshark的版本是否支持Lua 打开Wireshark,点击“HelpàAbout Wireshark”菜单,查看弹出的对话框,如果有“with Lua 5.1”表示支持 ...
- Mysql C语言API编程入门讲解
原文:Mysql C语言API编程入门讲解 软件开发中我们经常要访问数据库,存取数据,之前已经有网友提出让鸡啄米讲讲数据库编程的知识,本文就详细讲解如何使用Mysql的C语言API进行数据库编程. ...
- Unity进阶----Lua语言知识点(2018/11/08)
国内开发: 敏捷开发: 集中精力加班堆出来第一个版本 基本没啥大的bug 国外开发: 1).需求分析: 2).讨论 3).分模块 4).框架 5).画UML图(类图class function)(e- ...
随机推荐
- SpringCloud断路器(Hystrix)和服务降级案列
断路器(Hystrix) 为什么需要 Hystrix? 在微服务架构中,我们将业务拆分成一个个的服务,服务与服务之间可以相互调用(RPC).为了保证其高可用,单个服务又必须集群部署.由于网络原因或者自 ...
- 014_matlab读取ecxel(直接导入)
视频教程:https://v.qq.com/x/page/c3039b5htwx.html 资料下载:https://download.csdn.net/download/xiaoguoge11/12 ...
- v熬夜是场可怕的自杀
转载于 https://baike.baidu.com/vbaike/%E7%86%AC%E5%A4%9C%E6%98%AF%E5%9C%BA%E5%8F%AF%E6%80%95%E7%9A%84%E ...
- 验证码破解 | Selenium模拟登陆微博
模拟登陆微博相对来说,并不难.验证码是常规的5个随机数字字母的组合,识别起来也比较容易.主要是用到许多Selenium中的知识,如定位标签.输入信息.点击等.如对Selenium的使用并不熟悉,请先移 ...
- Netflix-mantis 实时数据流开发平台
mantis 是netflix 开源的已经在netflix 使用了多年的实时流处理平台,目前从官方文档的介绍,在netflix使用场景很多 使用场景 上下文报警 监控netflix 的微服务 异常追踪 ...
- fpm 打包跨平台rpm 包的一个问题
平时我们开发机器可能使用的是mac 系统,但是部署机器kennel是linux,默认会有一个操作系统的问题, 我们可以在打包的时候指定os版本信息 参考 --rpm-os linux 参考例子 一个构 ...
- STL next_permutation 全排列
调用方法: ]={,,,}; )){ ;i<;i++) printf("%d ",arr[i]); puts(""); } 测试效果: 注:可以看到1 2 ...
- 关于kmp算法
字符串匹配算法简称kmp 日常安利大佬博客(真的是一篇很好的文章) 觉得百度百科讲的也挺好 就是给出两个字符串a, b 求b在a中的所有位置 next数组:代表当前字符之前的字符串中,有多大长度的相同 ...
- 【LG2154】[SDOI2009]虔诚的墓主人
[LG2154][SDOI2009]虔诚的墓主人 题面 洛谷 题解 如果您没有看懂题,请反复阅读题面及样例 可以发现,对于某一个点,它的答案就是上下左右几个组合数乘起来. 这样直接做复杂度显然爆炸,考 ...
- SQL Server数据库应用技术
SQL Server数据库应用技术 SQL是Structured Query Language的缩写.SQL是操作命令集,是一种功能齐全的数据库语言.SQL功能强大.简单.易学.使用方便,已经成为了数 ...