943,670 Members | Top Members by Rank

Ad:
  • MySQL Discussion Thread
  • Unsolved
  • Views: 1333
  • MySQL RSS
Jun 15th, 2009
0

Update Stmt - Update same record multiple times

Expand Post »
Hi,

I've having a bit of difficulty with an update statement.

The following select statement returns 34 records...

MySQL Syntax (Toggle Plain Text)
  1. SELECT b.* FROM budget as b
  2. INNER JOIN ntime t on b.feeearnerkey=t.feeearnerkey
  3. INNER JOIN DATE d on t.datekey=d.datekey
  4. INNER JOIN acts a on t.activitykey=a.activitykey
  5. WHERE a.activitycode IN (11,12,13,14,15,16,17,18)
  6. AND b.fiscalmonth=d.fiscalmonthnum
  7. AND b.fiscalyear=d.fiscalyearnum
  8. AND b.budgetkey=1
  9. AND t.feeearnerkey=520

If I add a distinct clause to the statement, only 9 records are returned from the budget table.

I'm attempting to update the budget table 34 times, which means updating the same record from the budget table multiple times in the same update statement.

I've tried variations of the update statement, but only 9 records are ever updated.

MySQL Syntax (Toggle Plain Text)
  1. UPDATE budget as b
  2. INNER JOIN ntime t on b.feeearnerkey=t.feeearnerkey
  3. INNER JOIN DATE d on t.datekey=d.datekey
  4. INNER JOIN acts a on t.activitykey=a.activitykey
  5. SET b.budgetamount=b.budgetamount - (t.minutes / 60)
  6. WHERE a.activitycode IN (11,12,13,14,15,16,17,18)
  7. AND b.fiscalmonth=d.fiscalmonthnum
  8. AND b.fiscalyear=d.fiscalyearnum
  9. AND b.budgetkey=1
  10. AND t.feeearnerkey=520
  11.  
  12. UPDATE TIME t
  13. INNER JOIN budget b on t.feeearnerkey=b.feeearnerkey
  14. INNER JOIN DATE d on t.datekey=d.datekey
  15. INNER JOIN acts a on t.activitykey=a.activitykey
  16. SET b.budgetamount=b.budgetamount - (t.minutes / 60)
  17. WHERE a.activitycode IN (11,12,13,14,15,16,17,18)
  18. AND b.fiscalmonth=d.fiscalmonthnum
  19. AND b.fiscalyear=d.fiscalyearnum
  20. AND b.budgetkey=1
  21. AND t.feeearnerkey=520

Can anyone give any advice on how to go about updating the 34 records in one update statement.

Many Thanks!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nickj is offline Offline
7 posts
since Jun 2009
Jun 15th, 2009
0

Re: Update Stmt - Update same record multiple times

Quote ...
If I add a distinct clause to the statement, only 9 records are returned from the budget table.
are you sure you are needing to do 34 updates, 9 records only need 9 updates

if the update isn't working properly, can you give us a little more of the schema and also what the distinct column is?
Reputation Points: 133
Solved Threads: 141
Veteran Poster
dickersonka is offline Offline
1,162 posts
since Aug 2008
Jun 16th, 2009
0

Re: Update Stmt - Update same record multiple times

yes, I definately need to update the budget table 34 times.

The 'budgetamount' field stores an hourly target value for a particular month & year. I need to deduct any minutes (t.minutes / 60) that exist in the 'time' table for the particular month and year.

So although in the budget table, there can only be one entry for a particular month and year, many entries can exist in the time table for that particular month and year.

Maybe I need to apply some kind of aggregation to the time table before deducting from the budgetamount value?
Last edited by nickj; Jun 16th, 2009 at 3:56 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nickj is offline Offline
7 posts
since Jun 2009
Jun 16th, 2009
0

Re: Update Stmt - Update same record multiple times

ahhh, so for every record that exists in acts you are needing an update

my advice would be to use a sql 'for each' loop, you can use a cursor and iterate through each row and execute the update statement in there
Reputation Points: 133
Solved Threads: 141
Veteran Poster
dickersonka is offline Offline
1,162 posts
since Aug 2008
Jun 16th, 2009
0

Re: Update Stmt - Update same record multiple times

not acts, but for every record that exists in 'time' that has a certain code in act, update the corresponding budget record.

wanted to avoid cursors but looks like this is the only option.

cheers.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nickj is offline Offline
7 posts
since Jun 2009
Jun 17th, 2009
0

Re: Update Stmt - Update same record multiple times

I think I can achieve what I want with a update statement and an aggregate function in a correlated subquery.

...so don't need to go down the route of cursors / SP's, which is a good job as the version i'm working on doesn't support SP's!

Reputation Points: 10
Solved Threads: 0
Newbie Poster
nickj is offline Offline
7 posts
since Jun 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MySQL Forum Timeline: Can Crystal Reports be used with MySQL?
Next Thread in MySQL Forum Timeline: Supplied argument is not a valid MySQL result resource





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC