math.max question/issue

Hello, I’m trying to find the largest number inside a table returned in mysql.

Right now this is what I’m doing:



tmysql.query("SELECT `id` FROM `chat_box`", function( AddNum ) 
    PrintTable(AddNum)
end)


This returns:



1:
		1	=	2
2:
		1	=	3
3:
		1	=	4
4:
		1	=	5
5:
		1	=	6
6:
		1	=	7
7:
		1	=	8
8:
		1	=	9
9:
		1	=	10


print(math.max(unpack(AddNum))) gives me:



bad argument #1 to 'max' (number expected, got table)


and
print(math.max(unpack(AddNum[1]))) returns:



>2


I’ve tried printing math.max(unpack(AddNum)) but I can’t get the highest value (which in this case SHOULD be 10)

Does anyone know how I could do this? I’ve tried about everything I can think of.

It seems like there’re nested tables in each index of the main table, so I guess try this:


local tab = {}
for k, v in pairs( AddNum ) do
	table.insert( tab, v[1] )
end
print( math.max( unpack( tab ) ) )

That should work
However, if there is for some reason multiple numbers at each index, you’ll have to edit that to have another nested for loop running through the table stored at variable v

Awesome, this seems to work just fine!

Thanks.