You probably need to take a look at SELECT functions to work out what I'm talking about.
For example:
SELECT CURRENT_DATE()
might return 2004-05-27
Anything you can SELECT can be ALSO be used in an UPDATE or INSERT statement.
UPDATE mytable SET lastupdate = CURRENT_DATE()
See how that works?
You can do calculations using different fields in the table. If you have a column named total and a column called thisweek, you could write
UPDATE mytable SET total = total+thisweek
You can also do calculations on the fly:
UPDATE mytable SET tax=total*.06
Now things get interesting when you start COMBINING functions:
For example DATEDIFF() returns the number of days between 2 dates -- for example to calculate the interest due on a loan, you
take the Yearly interest rate,
divide by the 365 to get the daily rate
find the number of days between now and the last calculation
multiply the principal daily interest rate number of days:
UPDATE mytable SET interestDue=DATEDIFF(lastCalculation,CURRENT_DATE())(interestRate/365)principalAmount
You can combine some updates into one big update:
UPDATE mytable SET interestDue=DATEDIFF(lastCalculation,CURRENT_DATE())(interestRate/365)principalAmount,
lastupdate = CURRENT_DATE(),
total = total+thisweek,
tax=total*.06
To make things really intersting you can do if-then-else type work on the fly:
Suppose you wanted to update a column in your table to reflect the values of certain fields
Like:
Dear Mister Jones, You owe us $5.00.
Dear Ms Smith, You have no balance.
Dear Ms Black, You owe us $567.33.
Etc.
You can use IF constructions:
SELECT IF(customerType='M','Mister','Ms.') FROM mytable.
You can concat strings:
SELECT CONCAT ('You have a balance due of $',balancedue,'.')
You can mix these functions and use them in an update:
UPDATE mytable
SET messageText= CONCAT('Dear ',
IF(customerType='M','Mister','Ms.'),
lastName,
',',
IF(balanceDue>0,
CONCAT('You owe us $',balanceDue,'.'),
'You have no balance.')
)
And so on.
Hope this gets you started.