How would I make this code snippet add 10 to the value of "goals"?
if (buffer.empty())
break;
buffer.erase(buffer.size() - 1);
async_query("insert into " + db_name("files") + " ("
+ db_name("player") + ", "
+ db_name("goals") + ", "
+ db_name("completed") + ", "
+ db_name("fid") + ") values " + buffer
+ " on duplicate key update" + " "
+ db_name("player") + " = values("
+ db_name("player") + ")," + " "
+ db_name("goals") + " = values("
+ db_name("goals") + ")," + " "
+ db_name("completed") + " = values("
+ db_name("completed") + "),"
+ " mtime = unix_timestamp()");
buffer.clear();
First of all, this is pure MySQL. It won't work on other SQL engines.
Besides, our question is pretty ambiguous:
Do you want the new value of goals
to be incremented by 10 if the insert hits an existing fid
, or retain the old value and increment it by 10?
Or do you want to put the new value of goal
+ 10 in both cases?
Once the C++ obfuscation removed, your query boils down to :
insert into $db_name (player, goals, completed, fid) values $buffer
on duplicate key update
player = values(player),
goals = values(goals),
completed = values(completed),
mtime = unix_timestamp()
(variables have been represented by $xxx
)
I assume fid
is the unique index or primary key that will trigger the on duplicate key
.
I hope you don't have multiple unique indexes, or else the request will possibly modify only the first record that matches the entered values.
Assuming fid
is the key, what your request do is erase the player
, goals
and completed
fields (and update a timestamp) if a duplicate fid
is passed.
This seems an ungodly thing to do to me, but surely you have your reasons.
I also wonder why you don't update the timestamp if a new record is created, but that's not the object of your question...
So basically you have 3 possibilities here:
goals
to new goal + 10 in any caseThis would require to tweak the contents of your mysterious buffer, to add 10 to the new goal value.
Your code would then work without other modifications.
goals
to a new value for a new record, and to the same new value + 10 for an old recordinsert into $db_name (player, goals, completed, fid) values $buffer
on duplicate key update
player = values(player),
goals = values(goals) + 10,
completed = values(completed),
mtime = unix_timestamp()
goals
to a new value for a new record, and increment the old goals value by 10 for an old recordinsert into $db_name (player, goals, completed, fid) values $buffer
on duplicate key update
player = values(player),
goals = goals + 10,
completed = values(completed),
mtime = unix_timestamp()
Since I fail to understand what purpose is served by this query, I leave you the choice of the solution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With