MYSQL Right syntax for this
#1

pawn Code:
UPDATE `player_inventory` SET `amount` = `amount` + (`amount` WHERE `id` = '%d')
                                      WHERE `id` = '%d'
What's the right syntax for this? I'm trying to do a combination between same column and same table but in diff rows

(this is for weapon/ammo combination)
Reply
#2

A subquery needs to be a full fledged query.

PHP Code:
UPDATE `player_inventorySET `amount` = `amount` + (SELECT `amountFROM player_inventory WHERE `id` = %dWHERE `id` = %
And oh, don't use quotes around integer values. It prevents indexes from being used which will needlessly slow down your queries.
Reply
#3

Quote:
Originally Posted by Vince
View Post
A subquery needs to be a full fledged query.

PHP Code:
UPDATE `player_inventorySET `amount` = `amount` + (SELECT `amountFROM player_inventory WHERE `id` = %dWHERE `id` = %
And oh, don't use quotes around integer values. It prevents indexes from being used which will needlessly slow down your queries.
Yeah that was my first attempt but when I use it I get

#1093 - Table 'player_inventory' is specified twice, both as a target for 'UPDATE' and as a separate source for data
Reply
#4

Oh. Uhm, well it is a rather strange query. Do you mind showing your data structure? Perhaps there is better way to do it.
Reply
#5

Quote:
Originally Posted by Vince
View Post
Oh. Uhm, well it is a rather strange query. Do you mind showing your data structure? Perhaps there is better way to do it.
https://pastebin.com/2PycN5pr
Reply
#6

Hmm, are you trying to add two identical rows together? Same player, same item id? Because I'm not quite grasping the context.

What you should probably do is:
a) remove the id field and the keys associated with it.
b) put the primary key on the combination of (item_id, pid) to make that combination unique.

Note that the primary key is by definition unique. An extra unique attribute is redundant. Also consider switching to the InnoDB engine. It's much faster and it uses row locks instead of table locks.
Reply
#7

its not the same item ID. They have nothing in commom besides being in the same table and same PID. Its a combination system for items like fishroad and worms, weapon and ammo...

i'll switch for InnoDB ty. Maybe a JOIN or something would do the job in that case?

I think this fixed my problem

https://pastebin.com/quM3r2e2
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)