Click here to Skip to main content
16,020,567 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I use a mysql 5.7 database.

I want to create a job that automatically subtracts the money for the rented cars from a player's wallet.

I got a problem with updating the players amount of money for it's rented car(s). Only the costs for the first car, the join throws back is subtracted. Is a cross table update the right way to do it or do i need a loop?

What I have tried:

SQL
UPDATE player p 
JOIN garage g ON p.id = garage.owner 
JOIN car c ON c.id = garage.car
SET p.money = p.money - c.rentrate, g.lastpaidrent = NOW()
WHERE c.rented = '1' AND TIMESTAMPDIFF(HOUR, c.lastpaidrent, NOW()) >= 24 AND p.money >= c.rentrate
Posted
Updated 14-Feb-17 18:30pm
v6

1 solution

UPDATE player p
set p.money = p.money - c.rentrate,
from Player p inner join garage G on p.id = g.owner
inner join Car c on c.id = g.car
where C.rented = 1 and TIMESTAMPDIFF(HOUR, c.lastpaidrent, NOW()) >= 24 AND p.money >= c.rentrate
 
Share this answer
 
Comments
fcr33p 15-Feb-17 15:35pm    
Not yet what i need. if I correct your syntax, you just changed the join order what doesn't solve my problem

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900