Raised This Month: $51 Target: $400
 12% 

Mysql replace insert delete duplicate


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
4ever16
Veteran Member
Join Date: Apr 2015
Old 03-14-2020 , 06:59   Mysql replace insert delete duplicate
Reply With Quote #1

I have a wierd problem in my head i know the solution but something is not adding upp.

Code.
PHP Code:
 formatexszQuery3799"REPLACE INTO `today` (`player_id2`, `player_name2`, `kills2`, `deaths2`, `points2`) VALUES ('%s', '%s', '%d', '%d', '%d');"steamGetSecureName(Name), pTempKills[id], pTempDeaths[id], pTempPoints[id]); "ON DUPLICATE KEY UPDATE `text1` = VALUES ('player_id2');"steam
So basicly this code works. It creates if it doesnt exists. It also updates to new values.

BUT.

When CS 1.6 map restarts values starts from 0 so basicly it deletes old values and adds new values.

I want to REPLACE values without DELETING values.
I checked the plugin .SMA and i don't see anything wrong there.

I don't understand why it DELETES and RESTARTS VALUE COUNT when map change is made or when plugin is restarted?!

How can i REPLACE BUT NEVER DELETE OLD VALUES? Thanks!

Last edited by 4ever16; 03-14-2020 at 07:31.
4ever16 is offline
4ever16
Veteran Member
Join Date: Apr 2015
Old 03-14-2020 , 07:11   Re: Mysql replace insert delete duplicate
Reply With Quote #2

I feel like i need to use DUPLICATE or something.

Can someone help me out in someway cause im not thinking "right" right now.

Last edited by 4ever16; 03-14-2020 at 07:17.
4ever16 is offline
jimaway
Heeeere's Jimmy!
Join Date: Jan 2009
Location: Estonia
Old 03-14-2020 , 10:21   Re: Mysql replace insert delete duplicate
Reply With Quote #3

use INSERT not REPLACE
jimaway is offline
Old 03-14-2020, 19:36
gabuch2
This message has been deleted by gabuch2. Reason: nvm
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 03-15-2020 , 14:12   Re: Mysql replace insert delete duplicate
Reply With Quote #4

INSERT will always add a new record so you will end up with dupes.

Ideally, you should use INSERT for a new player, and UPDATE for an existing player.

REPLACE INTO essentially covers both without the need to know whether a record already exists. But, to use it correctly, you need to take the PRIMARY KEY or UNIQUE index into consideration.
__________________
Bugsy is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 03-15-2020 , 14:18   Re: Mysql replace insert delete duplicate
Reply With Quote #5

INSERT should fail if the column is configured as unique and the data for that column already exists.

If you don't know that a key will exist and you want to create if it doesn't, you can use INSERT INTO ... ON DUPLICATE KEY UPDATE ... I use this to increment a counter. If it doesn't exist, it inserts a 1, if it already exists, it increments the value all in a single MySQL instruction.
__________________

Last edited by fysiks; 03-15-2020 at 14:22.
fysiks is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 03-15-2020 , 14:33   Re: Mysql replace insert delete duplicate
Reply With Quote #6

Here's an example:

CREATE TABLE `tblPlayers` ( `ID` INTEGER PRIMARY KEY AUTOINCREMENT, `SteamID` TEXT NOT NULL UNIQUE, `PlayerName` TEXT, `Kills` INTEGER )

REPLACE INTO tblPlayers (SteamID, PlayerName, Kills) VALUES ("STEAM_123","bugsy",5);
REPLACE INTO tblPlayers (SteamID, PlayerName, Kills) VALUES ("STEAM_123","bugsy123",25);
REPLACE INTO tblPlayers (SteamID, PlayerName, Kills) VALUES ("STEAM_123","bugsy456",31);

With each execution, it results in only 1 record for the player with a SteamID of 'STEAM_123', updating only the player name and kills value. Primary key ID changes, but that doesn't matter since it's just an autoincrementing ID.
__________________

Last edited by Bugsy; 03-15-2020 at 14:33.
Bugsy is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 12:06.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode