/    Sign up×
Community /Pin to ProfileBookmark

updating mysql database…

Basically, I’ve built a little system, that records all poker tournament info.

I’m having difficulty with the leaderboard section.

Everytime you add a new player and prize money, I want it to add the prizemoney to the existing amount of prizemoney.

E.g. if John smith had 300 in prizemoney. I enter his name again, but this time entering 200… his total should now be 500.

However when I update it, instead of putting 500, it puts the amount that I just entered… 200.

is there a way of getting around this, possible something to do with the UPDATE statement?

MANY THANKS IN ADVANCE!!!

Rapidz.

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMar 07.2007 — You can add to an existing value, something like:
[code=php]
$sql = "UPDATE table_name SET prizemoney = prizemoney + $prizeAmount WHERE player_id = $playerId";
[/code]
Copy linkTweet thisAlerts:
@NightShift58Mar 07.2007 — You can add to an existing value, something like:
[code=php]
$sql = "UPDATE table_name SET prizemoney = prizemoney + $prizeAmount WHERE player_id = $playerId";
[/code]
[/QUOTE]
Sorry, it's a pet peeve...
[code=php]
$sql = "UPDATE table_name SET prizemoney = prizemoney + $prizeAmount WHERE player_id = $playerId LIMIT 1";
[/code]
Copy linkTweet thisAlerts:
@NogDogMar 07.2007 — Sorry, it's a pet peeve...
[code=php]
$sql = "UPDATE table_name SET prizemoney = prizemoney + $prizeAmount WHERE player_id = $playerId LIMIT 1";
[/code]
[/QUOTE]

I'm assuming (because it's how I would do it) that player_id would be defined as unique in the table definition (probably as the primary key), therefore making LIMIT 1 redundant. Besides, if it is [i]not[/i] unique, how would you know that the first record updated would be the correct one? (Which then goes to make it obvious why the whole thing would not make sense unless player_id [i]was[/i] unique.)
Copy linkTweet thisAlerts:
@NightShift58Mar 07.2007 — We shouldn't confuse the two issues involved.

The issue of an ID being unique - or not - has more to do with inserting duplicate ID's into a table, or, better said, preventing the insertion of duplicate ID's.

The LIMIT clause is there to prevent a table or index scan - when retrieving data - once a match has been found, which is a different issue than inserting.

Granted, if the field were defined/indexed as unique, the issue would be moot as MySQL would automatically perform the limiting steps, without table or index scan (unless, maybe, if you had forced a different index).

If the field were not defined as such, based on the OP's description, it seemed clear to me that the LIMIT clause needed to be there, as the query intends to extend or grant credit to a single player. Not knowing if I can rely on MySQL to use an index that may or may not exist, I use LIMIT.

There's no penalty for using needlessly while there could be a huge bonus if needed. Additionally, it serves to "self-document" the query by advertising the intended result - if a LIMIT is called for, obviously, as in this specific case.
Copy linkTweet thisAlerts:
@NogDogMar 07.2007 — Eh...you still haven't convinced me. To each his own, I guess.

If the table is properly defined such that the column in the where clause is indexed as unique (e.g.: a primary key), then the DBMS already knows there can be only one match, and it will find it very quickly via the indexing. I just don't see enough potential gain (if any) to add the redundant limit clause to make me worry about changing my habits ("old dogs" and all that stuff).
Copy linkTweet thisAlerts:
@NightShift58Mar 07.2007 — If the table is properly defined such that ...[/QUOTE]I agree with you on that - as I mentioned in my own reply. But, as an old Army saying goes: If my aunt had a pair, she'd be my uncle...

I'm not trying to change your habits but this is something I've been doing since the 80's working on different DBMS (more often than not, required by the QC teams). There's Zero-Penalty and those 6 characters will often save the day. It's a potential performance saver - especially within the context of this forum.
×

Success!

Help @rapidz spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.21,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...