• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Solved SQL query to get sum of statistics in different tables

whitevo

Feeling good, thats what I do.
Joined
Jan 2, 2015
Messages
3,454
Solutions
1
Reaction score
627
Location
Estonia
I found similar questions, but all of them are unanswered and made like year+ ago, so I make new one :D

my function:
Code:
function onUse(player, item, frompos, itemEx, topos)
local itemID = item:getId()
local itemAID = item:getActionId()

local highscoreData = db.storeQuery("SELECT attempts, kills, deaths FROM highscores WHERE monster = 'dummy' GROUP BY monster")
local attempts = result.getNumber(highscoreData, "attempts")
local kills = result.getNumber(highscoreData, "kills")

print(attempts)
print(kills)
end

prints:
Code:
1
0

Problem is, I want to get the total of all the attempts made against dummy and kills and deaths.
I clearly have more attempts than 1. Why it only takes the first result it sees?
2z4e2ih.jpg
 
Use SUM(columnName).

Code:
local attempts, kills
local highscoreData = db.storeQuery("SELECT SUM(attempts) AS attempts, SUM(kills) AS kills, SUM(deaths) AS deaths FROM highscores WHERE monster = 'dummy' GROUP BY monster")
if highscoreData ~= false then
    attempts = result.getNumber(highscoreData, "attempts")
    kills = result.getNumber(highscoreData, "kills")
    result.free(highscoreData)
end

print(attempts)
print(kills)
 
Use SUM(columnName).

Code:
local attempts, kills
local highscoreData = db.storeQuery("SELECT SUM(attempts) AS attempts, SUM(kills) AS kills, SUM(deaths) AS deaths FROM highscores WHERE monster = 'dummy' GROUP BY monster")
if highscoreData ~= false then
    attempts = result.getNumber(highscoreData, "attempts")
    kills = result.getNumber(highscoreData, "kills")
    result.free(highscoreData)
end

print(attempts)
print(kills)
yeah, just found the answer too in google xD and tested it. and even counted it xD just to be sure. The group by seems to be unnecessary.

But now second question. How to reset some fields?
Because I didn't think bosses what I summon myself and remove myself will end up in highscores, but apparently my trollings had consequences.

What I want to do is:
if attempts < deaths+kills then attempts = 0, deaths=0, kills=0 end (but as QUERY)

BTW:
result.free(highscoreData)
What is that line for?

and what is the difference between:
if highscoreData then
if highscoreData ~= false then

EDIT:
Clearly does not work xD (using select right now to see what rows it would change)
Code:
SELECT `kills`, `deaths`, `attempts`, `monster` FROM `highscores` WHERE `attempts` < `deaths`+`attempts`

EDIT2:
OK i see the fail in my WHERE now.. trol..
Ok, problems, solved.
 
Last edited:
Back
Top