[lua, mysql] 将多条记录数据组合成一条sql插入语句(for mysql)

-- 演示将多条记录数据组合成一条sql插入语句(for mysql)

function getTpl0(tname)		-- 获取表各个字段
	local t = {
		tpl_pack = {"packId","itemId","`group`","num","rate","rateType"},
	}
	for k, v in pairs(t) do
		if tname == k then
			return t[k]
		end
	end
end

--tpl = {3813,10,0,2,0,1,1,0,350,5,220,6,0,0,0,0,154,0,0,0,210,80,29}
tpl9122 = {
-- "packId","itemId","`group`","num","rate","rateType"
	{9122, 3294, ‘‘, 1, 1, 2},
	{9122, 3295, ‘‘, 1, 1, 2},
	{9122, 3296, ‘‘, 1, 1, 2},
	{9122, 3297, ‘‘, 1, 1, 2},
	{9122, 3298, ‘‘, 1, 1, 2},

	{9122, 9004, ‘‘, 2, 4, 2},
	{9122, 117, ‘‘, 8, 4, 2},
	{9122, 118, ‘‘, 8, 4, 2},
	{9122, 119, ‘‘, 8, 4, 2},
	{9122, 120, ‘‘, 8, 4, 2},
	{9122, 322, ‘‘, 2, 4, 2},
	{9122, 160, ‘‘, 5, 5, 2},
	{9122, 327, ‘‘, 5, 5, 2},
	{9122, 2900, ‘‘, 1, 6, 2},
	{9122, 9101, ‘‘, 20, 7, 2},
	{9122, 115, ‘‘, 15, 10, 2},
	{9122, 114, ‘‘, 15, 12, 2},
	{9122, 112, ‘‘, 15, 13, 2},
	{9122, 113, ‘‘, 15, 13, 2},
}

tpl9123 = {
-- "packId","itemId","`group`","num","rate","rateType"
	{9123, 3299, ‘‘, 1, 1, 2},
	{9123, 3300, ‘‘, 1, 1, 2},
	{9123, 3301, ‘‘, 1, 1, 2},
	{9123, 3302, ‘‘, 1, 1, 2},
	{9123, 3303, ‘‘, 1, 1, 2},

	{9123, 9004, ‘‘, 2, 4, 2},
	{9123, 117, ‘‘, 8, 4, 2},
	{9123, 118, ‘‘, 8, 4, 2},
	{9123, 119, ‘‘, 8, 4, 2},
	{9123, 120, ‘‘, 8, 4, 2},
	{9123, 322, ‘‘, 2, 4, 2},
	{9123, 160, ‘‘, 5, 5, 2},
	{9123, 327, ‘‘, 5, 5, 2},
	{9123, 2900, ‘‘, 1, 6, 2},
	{9123, 9101, ‘‘, 20, 7, 2},
	{9123, 115, ‘‘, 15, 10, 2},
	{9123, 114, ‘‘, 15, 12, 2},
	{9123, 112, ‘‘, 15, 13, 2},
	{9123, 113, ‘‘, 15, 13, 2},
}

function createInsertSql(tname, tpl)
	local tpl0 = getTpl0(tname)		-- 获取表各个字段
	local ret = {}					-- 插入记录sql

	table.insert(ret, string.format("insert into `%s`(", tname))
	for k, v in pairs(tpl0) do
		if k > 1 then
			table.insert(ret, ",")
		end
		table.insert(ret, v)
	end
	table.insert(ret, ") values ")

	for k, v in pairs(tpl) do
		if k > 1 then
			table.insert(ret, ",")
		end
		table.insert(ret, "(")
		for k2, v2 in pairs(v) do
			if k2 > 1 then
				table.insert(ret, ",")
			end
			if type(v2) == "string" then
				table.insert(ret, string.format("‘%s‘", v2))
			else
				table.insert(ret, v2)
			end
		end
		table.insert(ret, ")")
	end
	table.insert(ret, ";")

	local result = table.concat(ret)	-- 最终的sql语句
	print(result)
	print()
end
createInsertSql("tpl_pack", tpl9122)
createInsertSql("tpl_pack", tpl9123)

最终的执行结果如下:

[[email protected] 6]$lua t1.lua
insert into `tpl_pack`(packId,itemId,`group`,num,rate,rateType) values (9122,3294,‘‘,1,1,2),(9122,3295,‘‘,1,1,2),(9122,3296,‘‘,1,1,2),(9122,3297,‘‘,1,1,2),(9122,3298,‘‘,1,1,2),(9122,9004,‘‘,2,4,2),(9122,117,‘‘,8,4,2),(9122,118,‘‘,8,4,2),(9122,119,‘‘,8,4,2),(9122,120,‘‘,8,4,2),(9122,322,‘‘,2,4,2),(9122,160,‘‘,5,5,2),(9122,327,‘‘,5,5,2),(9122,2900,‘‘,1,6,2),(9122,9101,‘‘,20,7,2),(9122,115,‘‘,15,10,2),(9122,114,‘‘,15,12,2),(9122,112,‘‘,15,13,2),(9122,113,‘‘,15,13,2);

insert into `tpl_pack`(packId,itemId,`group`,num,rate,rateType) values (9123,3299,‘‘,1,1,2),(9123,3300,‘‘,1,1,2),(9123,3301,‘‘,1,1,2),(9123,3302,‘‘,1,1,2),(9123,3303,‘‘,1,1,2),(9123,9004,‘‘,2,4,2),(9123,117,‘‘,8,4,2),(9123,118,‘‘,8,4,2),(9123,119,‘‘,8,4,2),(9123,120,‘‘,8,4,2),(9123,322,‘‘,2,4,2),(9123,160,‘‘,5,5,2),(9123,327,‘‘,5,5,2),(9123,2900,‘‘,1,6,2),(9123,9101,‘‘,20,7,2),(9123,115,‘‘,15,10,2),(9123,114,‘‘,15,12,2),(9123,112,‘‘,15,13,2),(9123,113,‘‘,15,13,2);
时间: 2024-10-07 07:04:48

[lua, mysql] 将多条记录数据组合成一条sql插入语句(for mysql)的相关文章

mysql 查询一条记录的下一条和上一条记录

如果ID是主键或者有索引,可以直接查找: 方法一: 查询上一条记录的SQL语句(如果有其他的查询条件记得加上other_conditions以免出现不必要的错误): select * from table_a where id = (select id from table_a where id < {$id} [and other_conditions] order by id desc limit 1) [and other_conditions]; 查询下一条记录的SQL语句(如果有其他的

Oracle查询当前记录的上一条记录或下一条记录

//查询当前记录的 //oracle 上一条记录select decode(decode(txbs,'无',null,txbs),lag(t.txbs, 1, 0) over(order by t.cjid),'是','否')from qb_app_rycj t //下一条记录select t.id, lead(t.id, 1, 0) over(order by t.sort,t.eidt_date) as p from cms_article t

sql查询上一条记录和下一条记录

上一条记录的SQL语句: select top 1 * from news where newsid<id order by newsid DESC 下一条记录的SQL语句: select top 1 * from news where newsid>id order by newsid ASC 开发中遇到需要在当前页面显示当前文章的上一篇文章和下一篇文章,百度了一下,搜索到以上SQL语句:

Oracle通过一个字段的值将一条记录拆分为多条记录

前言 之前遇到了一次这样的需求,当时没有记录,这一次又赶上了,简单的记录一下. 本文个人拙见,若有出入,请指出--来自菜的颤抖 场景 表A中存放了集装箱的信息,一个集装箱一条记录,表B中存放了对于集装箱操作的指令,一条指令包括多个集装箱箱号,通过分号;切割(TCIU2347687;XUTR3546865),现在的需求是,对于已经在指令表B中的集装箱,在查询表A时需要过滤掉. 很容易想到的是not in, 然而分号分割. 其次,not like,然而[Err] ORA-01427: 单行子查询返回

SQL授权语句(MySQL基本语句)

看他们网上的,写得都是千篇一律,同时,好多也写得不是很好,下面是我自己总结的有关mysql的使用细节,也是我在学习过程中的一些记录吧,希望对你有点帮助,后面有关存储过程等相关操作还没有总结好,下次总结好了再发给你吧,呵呵~~~~~ MySql学习笔记 MySql概述:MySql是一个种关联数据库管理系统,所谓关联数据库就是将数据保存在不同的表中,而不是将所有数据放在一个大的仓库中.这样就增加了速度与提高了灵活性.并且MySql软件是一个开放源码软件. 注意,MySql所支持的TimeStamp的

MySQL用户管理、sql常用语句、mysql备份与恢复

MySQL用户管理 创建用户 grant all on *.* to 'user1'@'localhost' identified by '123456'; grant all on db1.* to 'user2'@'%' identified by '123456'; //创建user2用户,所有ip都能登录,指定权限为db1库下的所有表: flush privileges; 刷新授权 .:表示所有库和表:user1:用户名:localhost:登录ip,默认localhost为本机登录ip

mysql怎么查询一条记录的前一条记录和后一条记录

上一条:select * from 表 where 数据id<@当前显示数据id order by 数据_id asc) limit 1下一条:select * from 表 where 数据id>@当前显示数据id order by 数据_id desc) limit 1 mysql 里面不支持 select top

oracle将多条数据合并成一条

select  t.col1,t.col2  ,wmsys.wm_concat(t.col3||' ') AS col3 , wmsys.wm_concat(t.col4||'') as col4,wmsys.wm_concat(t.col5||'') as col5,wmsys.wm_concat(t.col6||'') as col6 from tablename t GROUP BY  t.col1,t.col2 ;

个人工作记录---工作中遇到的sql查询语句解析

在工作中写了人生的第一个查询语句,虽然是在原有基础上改的,但仍然学到了不少知识 代码: select distinct m.id, (select z.jianc from model_zuzjg z where z.id=m.huoz_id) as huoz, (select mingc from model_pinz where id=m.meiz_id) as meiz, ifnull((select kc.shul from yw_kuczt kc where kc.meiduo_id=