Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

is there a way to use arithmetic operators inside a record?

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
i have a site where we have reviews. if you want to see it. our reviews r dynamic and we have a numeric rating system where the overall score is the average between 5 sub scores. in my script the score is calculated inside my php script. but i want the users to be able to sort the scores with the ORDER BY thing but i dont have an overall score field. so im wondering if there's a way to add a field that would add up the 5 subscores and average them. could someone help?
 
No, it won't. But you don't have to. You can give the calculated field a name in your SELECT statment:

Given this table:
[tt]
Code:
+-------+------+------+------+------+------+
| title | s1   | s2   | s3   | s4   | s5   |
+-------+------+------+------+------+------+
| a     |    5 |    5 |    5 |    5 |    5 |
| b     |    4 |    4 |    4 |    4 |    4 |
| c     |    3 |    3 |    3 |    3 |    3 |
| d     |    2 |    2 |    2 |    2 |    2 |
| e     |    1 |    1 |    1 |    1 |    1 |
+-------+------+------+------+------+------+
[/tt]


The statement:
[tt]
Code:
SELECT title, ((s1 + s2 + s3 + s4 + s5)/5) AS average FROM scores ORDER BY average;
[/tt]

Will return:
[tt]
Code:
+-------+---------+
| title | average |
+-------+---------+
| e     |    1.00 |
| d     |    2.00 |
| c     |    3.00 |
| b     |    4.00 |
| a     |    5.00 |
+-------+---------+
[/tt]

Is this what you're looking for?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top