-1

How to run query in php for sum of two or more column in horizintally.

suppose i have table like

srno   name   english  maths  science  TotalMarks
1      rohit  52       52     52           ?

so how to get sum of all marks. and show in total marks field. by php code.

GMB
  • 216,147
  • 25
  • 84
  • 135
TechNil
  • 13
  • 1

4 Answers4

1

The correct approach would be somewhat as follows:

DROP TABLE IF EXISTS my_table;

CREATE TABLE my_table
(id SERIAL PRIMARY KEY
,srno INT NOT NULL
,subject VARCHAR(20) NOT NULL
,mark INT NOT NULL
);

INSERT INTO my_table VALUES
(1,1,'english',52),
(2,1,'maths',52),
(3,1,'science',52);

SELECT srno
     , SUM(mark) total 
  FROM my_table
 GROUP 
    BY srno;
    +------+-------+
    | srno | total |
    +------+-------+
    |    1 |   156 |
    +------+-------+
Strawberry
  • 33,750
  • 13
  • 40
  • 57
0

Try something like this:

SELECT srno, name, english, maths, science, (english+maths+science) As TotalMarks FROM `table_name`

Check also this topic: How to SUM two fields within an SQL query

0

Try this :

SELECT *, (english + maths + science) AS Total FROM table;

0

Well You can try these two methods:

select * from Your_Table_Name;

and when you display your result, you can add all these values like:

srno   name   english  maths  science  TotalMarks
$id    $name  $english $maths $science ($english+$maths+$science)

OR

select id, name, english, math, science, (english+maths+science) as Total from Your_Table_Name;
SKYz
  • 67
  • 6