简体   繁体   English

汇总mysql表中一行中的列值

[英]summing column values in a row in a table in mysql

I am new to mysql.我是 mysql 的新手。 I am in need to sum the column values of he same row in table and update the summed value in the same table using mysql.我需要对表中同一行的列值求和,并使用 mysql 更新同一个表中的总和值。 Table name is Students表名是学生

Name     roll_no    mark1      mark2     mark3      total    percentage
zxc      001         10          20        30         ?          ?
asd      002         20          30        50         ?          ?

I need mysql query to find total and percentage and update it in the corresponding columns.我需要 mysql 查询来查找总数和百分比并在相应的列中更新它。 After doing this if I do SELECT * from Students .这样做之后,如果我SELECT * from Students执行SELECT * from Students It has to display the following它必须显示以下内容

Name     roll_no    mark1      mark2     mark3      total    percentage
zxc      001         10          20        30         60          60
asd      002         20          30        50         100         100

You need to add those col values for total value and for percentage if I am correct for each subject 100 marks and to value is devides by 300 here(for 3 subjects)如果我对每个科目的正确率是 100 分,那么您需要为总值和百分比添加这些 col 值,并且此处的值除以 300(对于 3 个科目)

SELECT roll_no, mark1, mark2,mark3,   
mark1+mark2+mark3 as total,(mark1+mark2+mark3)*100/300 percentage
    From students

But as per your logic percentage is something like below但根据您的逻辑百分比如下所示

SELECT roll_no, mark1, mark2,mark3,   
    mark1+mark2+mark3 as total,(mark1+mark2+mark3)*100/100 percentage
        From students

But first one seems more meaningful than latter但第一个似乎比后者更有意义

As per OP comments, data should be updated in the columns根据 OP 评论,应更新列中的数据

Update students set total=mark1+mark2+mark3, 
Percentage=(mark1+mark2+mark3)*100/300

Now select the table, you will get calculated total and percentage现在选择表格,您将获得计算的总数和百分比

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM