[英]how to run sql query for sum of two or more rows in php file
How to run query in php for sum of two or more column in horizintally.如何在 php 中运行查询以获取水平两列或更多列的总和。
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.通过 php 代码。
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 |
+------+-------+
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另请查看此主题: 如何在 SQL 查询中对两个字段求和
Try this :尝试这个 :
SELECT *, (english + maths + science) AS Total FROM table; SELECT *, (english + maths + science) AS Total FROM table;
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.