UAEpro Posted February 25, 2012 Share Posted February 25, 2012 (edited) hello for example i got a table record player action ------------------------------------------------------------- player name ------------- serial ---------------- action ------------------------------------------------------------- --UAEpro-----------------ABCD-----------------Send Money ---UAEpro2--------------ABCD-----------------got hunter --someoneelse---------BASDF-----------------Send Money ok i want to remove the duplicated serial i mean i want to be in the table different serials so if player has more then action in differents names he will show only 1 per serial Solved by MySQL Experts SELECT * FROM ( select * from `my_table` order by timestamp desc ) as my_table_tmp group by catid order by nid desc Edited March 2, 2012 by Guest Link to comment
arezu Posted February 25, 2012 Share Posted February 25, 2012 function tableUnique(tab, field) local _tempTab = tab for i = 1, #tab do local index = tableSearch(tab, field, tab[i][field]) if(index ~= false and index ~= i)then table.remove(_tempTab, i) end end return _tempTab end function tableSearch(tab, field, value) for i = 1, #tab do if(tab[i][field] == value)then return i end end return false end local ourTable = {} ourTable[1].name = "name1" ourTable[1].serial = "FAAAF" ourTable[2].name = "name2" ourTable[2].serial = "FAAAF" ourTable = tableUnique(ourTable, "serial") Link to comment
UAEpro Posted February 25, 2012 Author Share Posted February 25, 2012 he delete some names but not all Link to comment
arezu Posted February 25, 2012 Share Posted February 25, 2012 he delete some names but not all show how you use it Link to comment
UAEpro Posted February 25, 2012 Author Share Posted February 25, 2012 _top10 = tableUnique(_top, "Serial") Link to comment
arezu Posted February 25, 2012 Share Posted February 25, 2012 _top10 = tableUnique(_top, "Serial") show also how your _top table looks like Link to comment
Kenix Posted February 25, 2012 Share Posted February 25, 2012 function table.replace( t,condition,repl ) if type( t ) == "table" then if type( condition ) == "number" or type( condition ) == "string" or type( condition ) == "boolean" and type( repl ) == "number" or type( repl ) == "string" or type( repl ) == "boolean" then local old = t local new = { } for i,v in pairs( old ) do if v == condition then new[ i ] = repl else new[ i ] = v end end return new,old elseif type( condition ) == "table" and type( repl ) == "table" then local old = t local new = { } for i,v in pairs( old ) do if v == condition[ i ] then new[ i ] = repl[ i ] else new[ i ] = v end end return new,old end return false end return false end local ourTable = { } ourTable.name = "name1" ourTable.serial = "FAAAF" for _,v in pairs( table.replace( ourTable,'name1','_' ) ) do print( v ) end --[[ Output: _ FAAAF ]] Read my code and comments. Use my function table.replace. You mean it? Link to comment
UAEpro Posted March 1, 2012 Author Share Posted March 1, 2012 function tableUnique(tab, field) local _tempTab = tab for i = 1, #tab do local index = tableSearch(tab, field, tab[i][field]) if(index ~= false or index ~= nil and index ~= i)then table.remove(_tempTab, i) end end return _tempTab end function tableSearch(tab, field, value) for i = 1, #tab do if(tab[i][field] == value)then return i end end return false end line 4 = attempt to index field '?' (a nil value) Link to comment
arezu Posted March 1, 2012 Share Posted March 1, 2012 function tableUnique(tab, field) local _tempTab = tab for i = 1, #tab do local index = tableSearch(tab, field, tab[i][field]) if(index ~= false or index ~= nil and index ~= i)then table.remove(_tempTab, i) end end return _tempTab end function tableSearch(tab, field, value) for i = 1, #tab do if(tab[i][field] == value)then return i end end return false end line 4 = attempt to index field '?' (a nil value) those functions only works when your table looks a certain way, like the example i posted. use this instead if your table looks different (tested): function tableUnique(tab, field) local _tempTab = tab for k, v in pairs(tab) do local index = tableSearch(tab, field, tab[k][field]) if(index ~= false and index ~= k)then table.remove(_tempTab, k) end end return _tempTab end function tableSearch(tab, field, value) for k, v in pairs(tab) do if(tab[k][field] == value)then return k end end return false end local test = {} test[1] = {} test[1].name = "arezu" test[1].serial = "ABCDEFG" test[2] = {} test[2].name = "arezu2" test[2].serial = "ABCDEFG" test[3] = {} test[3].name = "anotherplayer" test[3].serial = "FEGES" test[4] = {} test[4].name = "anotherplayer2" test[4].serial = "FEGES" test[5] = {} test[5].name = "another" test[5].serial = "FEGESZ" test = tableUnique(test, "serial") for k, value in pairs(test) do outputChatBox("name: "..test[k].name.." serial: "..test[k].serial) end if it doesn't work, make sure your table is multidimensional (like this: table[1]["name"], or table[1].name (the same)) Link to comment
UAEpro Posted March 1, 2012 Author Share Posted March 1, 2012 my table look like this _top, { name = row.name; Money = row.Money; CTag = row.CTag; Serial = row.Serial; } Link to comment
arezu Posted March 1, 2012 Share Posted March 1, 2012 my table look like this _top, { name = row.name; Money = row.Money; CTag = row.CTag; Serial = row.Serial; } Are you trying to compare serial of a 1 size table with other table field's serial? what? If you are trying to compare serials, of course you need more serials to compare with. From the question you asked, your table should look like this: local myTable = {} myTable[1] = {} myTable[1].playerName = "UAEpro" myTable[1].serial = "ABCD" myTable[1].action = "Send Money" myTable[2] = {} myTable[2].playerName = "UAEpro2" myTable[2].serial = "ABCD" myTable[2].action = "got hunter" myTable[3] = {} myTable[3].playerName = "someoneelse" myTable[3].serial = "BASDF" myTable[3].action = "Send Money" Link to comment
UAEpro Posted March 1, 2012 Author Share Posted March 1, 2012 if i get the values the table look like this _myTable[1].name = "UAEpro" _myTable[1].serial = "ABC" _myTable[2].name = "UAEpro3" _myTable[2].serial = "ABC" _myTable[3].name = "someoneelse" _myTable[3].serail = "BAC" Link to comment
AGENT_STEELMEAT Posted March 2, 2012 Share Posted March 2, 2012 You could just prevent duplicate entries from being added in the first place... Link to comment
UAEpro Posted March 2, 2012 Author Share Posted March 2, 2012 You could just prevent duplicate entries from being added in the first place... it's not easy can you fix it ? Serial = getPlayerSerial local _tops = {} query = mysql_query(connect_mysql,"SELECT DISTINCT * FROM `"..tableName.."` ORDER BY `"..tableName.."`.`Money` ASC ") if query then while true do local row = mysql_fetch_assoc( query ) if not row then mysql_query(connect_mysql, "INSERT INTO `"..tableName.."`(name, Money, Date, Serial, CTag) VALUES('"..name.."', '"..Money.."', '"..Date.."', '"..Serial.."','".. CTag.."')") break end if row.Serial then if row.Serial == Serial then if row.Money then if tonumber(row.Money) >= tonumber(Money) then query2 = mysql_query(connect_mysql, "DELETE FROM `"..tableName.."` WHERE `Serial` = '"..Serial.."'") if (not query2) then outputDebugString("Error executing the query: (" .. mysql_errno(connect_mysql) .. ") " .. mysql_error(connect_mysql)) end query = mysql_query(connect_mysql, "INSERT INTO `"..tableName.."`(name, Money, Date, Serial, CTag) VALUES('"..name.."', '"..Money.."', '"..Date.."', '"..Serial.."','".. CTag.."')") else outputChatBox("sorry your Money is worse") end end end else query3 = mysql_query(connect_mysql, "INSERT INTO `"..tableName.."`(name, Money, Date, Serial, CTag) VALUES('"..name.."', '"..Money.."', '"..Date.."', '"..Serial.."','".. CTag.."')") if not query3 then outputDebugString("Error executing the query: (" .. mysql_errno(connect_mysql) .. ") " .. mysql_error(connect_mysql)) end end table.insert( _tops, { Money = row.Money; name = row.name; CTag = row.CTag; } ) end mysql_free_result( query ) i know it's spaghetti Link to comment
Kenix Posted March 2, 2012 Share Posted March 2, 2012 viewtopic.php?f=91&t=40807 I mean make the code refactoring. Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now