Complicated SQL Update need help

Discussion in 'PHP' started by lui2603, Jul 23, 2009.

  1. #1
    I'm having trouble thinking how this would be pulled off in SQL (or if its even possible)
    Here's the parts of the database you need to know:

    Users:
    --id
    --money

    Items:
    --id
    --moneygain

    Inv(entory):
    --id
    --user_id
    --item_id
    --quantity

    The script runs via cron job every 60 minutes and needs to globally update every users with their unique invtentory settings.
    What I'm trying to do is update Users.money with the sum of Items.moneygain * Inv.quantity

    What I have so far:
    
    mysql_query("UPDATE users 
    		JOIN (SELECT SUM(items.moneygain*inv.quantity) AS gainsum 
    	                FROM items 
    			JOIN inv ON inv.item_id=items.id)
    		AS calc 
    		SET users.money = calc.gainsum") or die(mysql_error());
    
    PHP:
    The problem with this is that it creates a sum of every moneygain and not just those specific to a user
    *I can't use a where clause to choose a user with a specific id because I need to update them all at once (with a single query if possible) and without having to use a "totalmoneygain" field in Users table which will be added to and subtracted from every time a user receives an item..
     
    lui2603, Jul 23, 2009 IP
  2. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #2

    There's no WHERE clause defining the select statement in the middle, so course that would happen. You'd either have to define a specific user, or somehow work the DISTINCT selector in there on a user id.
     
    kblessinggr, Jul 23, 2009 IP
  3. lui2603

    lui2603 Peon

    Messages:
    729
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Do you have an idea of how a distinct query would work in this case?
    As far as I know all it would do is select 1 row per user which is no good because users might have multiple items in the inventory table
     
    lui2603, Jul 23, 2009 IP
  4. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #4
    What is the relationship between the tables, specifically the Inventory and Items tables?

    Edit: Disregard, I misread the Inventory table.
     
    jestep, Jul 23, 2009 IP
  5. Evescence

    Evescence Active Member

    Messages:
    154
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #5
    Seems like hard work, can not help you.
     
    Evescence, Jul 23, 2009 IP
  6. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #6
    Try something like this:

    UPDATE Users
    LEFT JOIN Inventory ON Users.id = Inventory.user_id
    LEFT JOIN Items ON Inventory.item_id = Items.id
    SET money = (Items.moneygain * Inventory.quantity)
     
    jestep, Jul 23, 2009 IP
  7. lui2603

    lui2603 Peon

    Messages:
    729
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #7
    If you have any ideas for how the query should look please post it, doesnt have to be based off mine because it's probably completely wrong hah
     
    lui2603, Jul 23, 2009 IP
  8. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Correct, in which case you'd be better off looping each user (ie: doing a query to grab all the user ID, then doing a loop to run a query for each user)

    In the end you quickly realize that 2 or 3 queries is far more practical than trying to get it all combined into one.
     
    kblessinggr, Jul 23, 2009 IP
  9. lui2603

    lui2603 Peon

    Messages:
    729
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Thanks for helping but I tried this way and it only adds money based on the first inventory record for each user
     
    lui2603, Jul 23, 2009 IP
  10. lui2603

    lui2603 Peon

    Messages:
    729
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #10
    It would be more practical to code but wouldn't using 2 or 3 queries for each user id (could be thousands) use up a huge amount of resources?

     
    lui2603, Jul 23, 2009 IP
  11. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #11
    I see the problem. You could do it with a stored procedure. I don't think it's possible without being able to reference the user_id from the Inventory table.
     
    jestep, Jul 23, 2009 IP
  12. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Not much more than trying to make the SQL server handle that all in a single complex query. UPDATE/DELETE types of queries take up very little resources. And if you only pull the needed information from the user table (ie: just the id), you minimize the resource quite a bit, for example doing SELECT * FROM... on a few hundred thousand records, could take up a shitload of resources, but SELECT id FROM might only take up about a megabyte.

    Also a few thousand is not really a big deal at all, not like we're dealing with a MS Access database that can only handle 64K records (Where as MySQL could handle a few million easily), just a matter of formating your database structure wisely, and only retrieving the information you need.
     
    kblessinggr, Jul 23, 2009 IP