[lua, mysql] 將多條記錄資料群組合成一條sql插入語句(for mysql)

來源:互聯網
上載者:User

標籤:

-- 示範將多條記錄資料群組合成一條sql插入語句(for mysql)function getTpl0(tname)-- 擷取表各個欄位local t = {tpl_pack = {"packId","itemId","`group`","num","rate","rateType"},}for k, v in pairs(t) doif tname == k thenreturn t[k]endendend--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 = {}-- 插入記錄sqltable.insert(ret, string.format("insert into `%s`(", tname))for k, v in pairs(tpl0) doif k > 1 thentable.insert(ret, ",")endtable.insert(ret, v)endtable.insert(ret, ") values ")for k, v in pairs(tpl) doif k > 1 thentable.insert(ret, ",")endtable.insert(ret, "(")for k2, v2 in pairs(v) doif k2 > 1 thentable.insert(ret, ",")endif type(v2) == "string" thentable.insert(ret, string.format("‘%s‘", v2))elsetable.insert(ret, v2)endendtable.insert(ret, ")")endtable.insert(ret, ";")local result = table.concat(ret)-- 最終的sql語句print(result)print()endcreateInsertSql("tpl_pack", tpl9122)createInsertSql("tpl_pack", tpl9123)

 最終的執行結果如下:

[[email protected] 6]$lua t1.luainsert 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);

 

 

[lua, mysql] 將多條記錄資料群組合成一條sql插入語句(for mysql)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.