Solved

PHP MySQL update supquery

Posted on 2011-02-24
7
387 Views
Last Modified: 2012-05-11
I'm sure this is a pretty simple question, but I can't think of a solution at the moment.  I'm trying to update a decimal value, based on the current value of the field.

I want to reduce the amount in the field by the amount in a variable... something like this...

"currentrequest" is the current field...  So, basically I want to update current request to be it's current value minus 100.00.  

I've tried using a select subquery in there, and it tells me that I can't select the same table...  
The error I receive is: "You can't specify target table 'budget' for update in FROM clause".

Thanks for any shoves in the right direction!


$reqAmt=100.00;

$removeFromBudget=@mysql_query("
   UPDATE budget 
   SET currentrequest='currentrequest-".$reqAmt."' 
   WHERE acct='".$acctNum."' LIMIT 1");

Open in new window

0
Comment
Question by:cbastian-hill
  • 4
  • 3
7 Comments
 

Accepted Solution

by:
cbastian-hill earned 0 total points
ID: 34972441
Just found the answer...   Posting in case anyone else needs it...


$removeFromBudget=@mysql_query("
   UPDATE budget 
   SET currentrequest=((SELECT currentrequest)-".$reqAmt.")
   WHERE acct='".$acctNum."' LIMIT 1");

Open in new window

0
 
LVL 17

Expert Comment

by:nanharbison
ID: 34972454
Can you give us the whole query? I don't see a subquery here.
0
 

Author Comment

by:cbastian-hill
ID: 34972473
That is the whole query...  The sub query is in the SET currentrequest..... line...

Are you not seeing that line?  Or am I describing it incorrectly?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 17

Expert Comment

by:nanharbison
ID: 34972943
That's not a subquery, but glad you found the answer!
0
 

Author Comment

by:cbastian-hill
ID: 34972973
Interesting...  I was always taught that anything like that inside another query would be considered a subquery.  Would you know what it should be called, then?
0
 
LVL 17

Expert Comment

by:nanharbison
ID: 34973097
It just doesn't look like a subquery because UPDATE a table, set something equal to a value is a regular update query, right? But you do have to select a value first before you can change it by doing math on that value...hmm, not sure.Maybe you are right?
0
 

Author Comment

by:cbastian-hill
ID: 34973139
Hmmm....  That is what I was thinking...  The SELECT inside the UPDATE would be a subquery...  I'll have to go back to the books later, when I have some time, and see if I can find an exact definition of what makes a subquery...   Thanks for the info, either way!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now