Mathematical functions within the DB

Discussion in 'MySQL' started by ingilizdili, Oct 31, 2010.

  1. #1
    Hello;
    Are mathematical operations possible within a mysql table without PHP? I want all values in Column A to be multiplied by the values in column B and the results to appear in column C; and I want the process to repeat itself each time new values are entered. In other words, can we insert mathematical formulas into a mysql table like we do with excell?
    Thanks in advance.
     
    ingilizdili, Oct 31, 2010 IP
  2. Layoutzzz

    Layoutzzz Greenhorn

    Messages:
    78
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    18
    #2
    Hi,
    
    UPDATE`table` SET `c`=`a`*`b`
    
    Code (markup):
    where a,b,c - are column
     
    Layoutzzz, Nov 1, 2010 IP
  3. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #3
    jestep, Nov 1, 2010 IP
  4. plog

    plog Peon

    Messages:
    298
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #4
    Tables are for storing unique pieces of related data, not calculations or other redundancies. The easiest way to achieve this is also the proper way to achieve this--with a query. Leave your table alone and set up a query to display these 3 pieces of data.

    Your table should only have 2 fields: A & B. Field C should be generated everytime you run the query, by the query. This is what that query should look like:

    SELECT A, B, (A*B) AS C FROM TABLE;

    Then when you want to exctract or view your data you run the above query--you never have to mess with updating the whole Table.
     
    plog, Nov 1, 2010 IP
  5. ingilizdili

    ingilizdili Peon

    Messages:
    61
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I needed this in order to find and echo the highest value from among the values which are found after some mathematical calculations. I want to find out the percentage of correct answers, which I achieve by php like this:
    $per = ($row['correct'] / ($row['correct'] + $row['incorrect']))*'100';
    When I echo $per, I get a list. I don't want a list. I want only the highest value to be displayed so that I can point out who is the most successful.
     
    ingilizdili, Nov 1, 2010 IP
  6. plog

    plog Peon

    Messages:
    298
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #6
    I've re-written this response 7 times now trying to not sound like too much of a dick (believe it or not, this version is the least dickish I can sound)--but at some point you need a basic understanding of programming to get assistance. What you are trying to do has elementary solutions in both php and mysql. The solutions are so elementary that if you can't immediately see one of them, asking someone for the answer won't help you much because you don't have the base knowledge to implement them. Add to that the problems you are having retrieving and displaying data from your database and your initial question about storing mathematical functions in your database, I suggest finding someone who can accomplish this task/project for you because it is beyond your current ability.
     
    plog, Nov 1, 2010 IP
  7. ingilizdili

    ingilizdili Peon

    Messages:
    61
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Sorry for my ignorance but I am trying to learn. Sometimes you learn by reading step by step, and sometimes you learn by living. If you don't think a question is worth answering, then don't answer it.
     
    ingilizdili, Nov 1, 2010 IP
  8. plog

    plog Peon

    Messages:
    298
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #8
    In general you can solve this with mysql by creating a query that employs the calculation technique I showed in my first post along with the 'ORDER BY' keyword (http://www.tizag.com/mysqlTutorial/mysqlorderby.php) on that calculation.

    In general, you can also solve this with php by looping through all the records of your database, calculating each record's percentage and keeping track of which record has the highest percentage. Using the code you already have set up, this would mean creating a new variable to keep track of the highest percentage and testing each one against it to determine if it is higher than the highest you have encountered.
     
    plog, Nov 1, 2010 IP