Jump to content

Field Calculation


shaunk

Recommended Posts

Hopefully this is easy for womeone else:-

I've recently set up a database that contains a numbet of fields, say for arguments sake they are field1, field2, field3 etc....

Want I want to do is make field5 equal a value of the latest value entered in field1 minus the first value entered in field1.

ie: in the column field1 the first or starting entery is say 50 and as users interact with the database this value increases dependant on user input (don't worry about why) so the last value in column field1 could be say 65. I want to calculate the difference (15) and enter it into a new field.

I seem to be running in to problems with a syntax or something because I've tried many combinations like the following:-

<?php $firstvalue = SELECT field1 FROM databasename ORDER BY field1 ASC LIMIT 1 ;
$secondvalue = SELECT field1 FROM databasename ORDER BY field1 DESC LIMIT 1 ;
$valuerequired = $secondvalue - $firstvalue
echo "$valuerequired" ?>

Any help will be much appriciated I've run out of ideas.

Thanks Shaun
Link to comment
Share on other sites

you're close, but when you simply do a query, it returns a reference to the results, not the results themselves. so, you'd need to do this:
[code]
$first = mysql_result(mysql_query("SELECT field1 FROM tableName ORDER BY field1 ASC LIMIT 1"), 0, 'field1');
$last = mysql_result(mysql_query("SELECT field1 FROM tableName ORDER BY field1 DESC LIMIT 1"), 0, 'field1');

echo ($last - $first);
[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.