0

I have a tables in my database. The table contain 6 rows. Two rows TOTAL and AVERAGE are self inserted by the sum of other rows I have been usung php amd mysql to insert datas into the table and it works fine but now it requires that i have to use only nysql . how can i write it so that the sum perform fine?

i have the below codes to emphasize more.

 my_tb
 field1   field2   field3   field4  total  average
 10        12       32          5          sum    sum

my codes

$total = $abc1+$abc2+$abc3+$abc4;
                    $average= ( $abc1+$abc2+$abc3+$abc4 ) / 4;
                // write new data into database
                $sql = "INSERT INTO my_tb (field1, field2, field3, field4,total,average)
                  VALUES('" . $abc1 . "', '" . $abc2 . "',
                   '" . $abc3 . "',  '" . $abc4 . "', '" . $total . "',  '" . $average . "', '" . $total_score . "'  );";
                $query_new_user_insert = $this->db_connection->query($sql);

                // if user has been added successfully
                if ($query_new_user_insert) {
                    $this->messages[] = "Your data has been created successfully. You can now log in.";
                } else {
                    $this->errors[] = "Sorry, your data upload failed. Please go back and try again.";
                }
            }

            now i am using SQL only

            INSERT INTO `my_tb` (`field1`, `field2`,`field3`,`field4`,`total`,`average`,) VALUES
 ('10', '31',  '31',  '31' , 'field1+field2.....', 'field1+field2...../4');

 but it doesnt work. it inserts the varriables instead of its sum.

pls can someone tell me how to achieve this?

7
  • That's not how SQL works... you're going to have to use PHP... Commented Oct 29, 2014 at 20:27
  • what requires you to do this ... Commented Oct 29, 2014 at 20:28
  • how do I do it? because now I am external SQL sheet that I will import into my db. Commented Oct 29, 2014 at 20:30
  • the data involved is much. its not advice able to insert them one by one via a form Commented Oct 29, 2014 at 20:32
  • I don't fully understand what you are trying to achieve.. You look like you have what needs to be done. I mean if you just make $abc3 = $abc1+$abc2 you're going the right direction, but SQL does not add fields the way you're wanting to. Commented Oct 29, 2014 at 20:32

2 Answers 2

3

That's not a table with 6 rows, it's a table with 6 columns, of which you present just one row. It also seems strange in that it has no obvious keys, and it is seriously denormalized in that the total and average columns are functionally dependent on the other four.

It's not clear what your objective is here, but note that because of the functional dependency it is unnecessary to have the total and average columns at all. If you drop them then you can still query the total and average like so:

SELECT
  field1,
  field2,
  field3,
  field4,
  (field1 + field2 + field3 + field4) AS total,
  (sum / 4.0) AS average
FROM my_tb

Edited to rename columns to match the original table; sum -> total, avg -> average.

Sign up to request clarification or add additional context in comments.

8 Comments

see I can't figure out if the values are already in a table or if he is trying to get them there by another means of input. But this is a great answer for provided as of now.
assuming I drop them, how can I Order by total since I need it to display position s.
@Gentledozy As above, plus ORDER BY sum
Also, PLEASE add an auto-index IDENTITY, even if you don't use it anywhere. That will save major headaches later if you run into any corruption or duplication issues later on.
@Steve, asumming I have four students, how can it order by total and show who scored the highest since I am using select?
|
0

Mysql

INSERT INTO my_tb (field1, field2,field3,field4,total,average) VALUES (@abc1:='10', @abc2:='31', @abc3:='31', @abc4:='31' , @abc1+@abc2+@abc3+@abc4, (@abc1+@abc2+@abc3+@abc4)/4);

PHP

$sql = "INSERT INTO my_tb (field1, field2,field3,field4,total,average) VALUES (@abc1:='" . $abc1 . "', @abc2:='" . $abc2 . "', @abc3:='" . $abc3 . "', @abc4:='" . $abc4 . "' , @abc1+@abc2+@abc3+@abc4, (@abc1+@abc2+@abc3+@abc4)/4)";

I suppose you will get that you want.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.