mysql - Table insert unexisting values -


current situation tblgamepoints

id    gamenumber    playerid    game    points ----------------------------------------------- 1     1                       fps     150 2     1             b           fps     150 3     2                       fps     100 4     2             b           fps     75 

when starting gamenumber 3 add empty rows of game in case not allready have points (penalty points).

example 1:

id    gamenumber    playerid    game    points ----------------------------------------------- 1     1                       fps     150 2     1             b           fps     150 3     2                       fps     100 4     2             b           fps     75 

in example above, there aren't existing rows gamenumber 3 insert statement should insert games points 0 playerids

example 2:

id    gamenumber    playerid    game    points ----------------------------------------------- 1     1                       fps     150 2     1             b           fps     150 3     2                       fps     100 4     2             b           fps     75 5     3                       fps     -50 

in example 2 player allready has been awarded penalty points. insert should add gamenumber 3 rows players except allready have row 'gamenumber 3'.

current sql statement used

insert tblgamepoints(gamenumber,playerid,game,points)      select 3,tblplayer.id,'fps',0      tblplayer          left join tblgamepoints on tblplayer.id = tblgamepoints.playerid      tblplayer.game='fps' , validatedentry='1'          , ifnull(tblgamepoints.gamenumber, 0) <> 3 

current result of sql statement used adds entries games instead of game 3. adds 5-6 records in example instead of 1 or 2.

using not exists condition seems easier me:

insert tblgamepoints (gamenumber, playerid, game, points)  select      3, tblplayer.id, 'fps', 0         tblplayer        game = 'fps' ,              validatedentry='1' ,              not exists (select *                           tblgamepoints                          playerid = tblplayer.id , game = 3) 

Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -