繁体   English   中英

如何使用单个查询执行多个算术运算并将答案存储在同一表的两个不同列中

[英]How can I do more than one arithmetic operation using a single query and store the answer in two different columns on the same table

这将从数据库中选择数据,并对其进行处理

    $name = mysqli_real_escape_string($connect, $_GET['name']);
                if (isset($_GET['name'])) {
                # code...

                        echo "</br> <br>";

                $count="SELECT id,employee_name, employee_salary, employee_age, class_id, (( employee_salary + employee_age + employee_allowance)/(100) * 100) AS staff_percentage, (employee_salary + employee_allowance + employee_age ) as staff_total FROM `employee`
                ";

                echo "<table>";
                echo "<tr><th>id</th><th>employee name</th><th>employee salary</th><th>employee age</th><th>employee allowance</th><th>staff total</th><th>staff total</th></tr>";

                    foreach ($connect->query($count) as $row) {
                    echo "<tr ><td>$row[id]</td><td>$row[employee_name]</td><td>$row[employee_salary]</td><td>$row[employee_age]</td><td>$row[employee_allowance]</td><td>$row[staff_percentage]%</td><td>$row[staff_total]</td></tr>";

在这个地方,我想捕获已经处理过的数据并将其发送到数据库,不仅是为了获得一个ID,还包括了所有的麻烦。

                    $sql = "UPDATE `employee` set employee_name = '" . $row["employee_name"] . "', employee_salary='" . $row["employee_salary"]."', employee_allowance='" . $row["employee_age"]."', class_id='" . $row["class_id"]. "', class_total;='" . $row["staff_total"]."', staff_percentage='" . $row["staff_percentage"]. "'";
                                $result = $connect->query($sql);

                                print "<script>alert('you have been registerded succesfully!');</script>";
                                        }

            echo "</table>";

            }

只需像这样在同一个查询中执行两个计算

Select  test, exam, 
        (test + exam) as total,
        ((test + exam)/100)*100 as percentage       
FROM school;

只需将查询的两个算术部分结合在一起即可:

SELECT test, exam, 
(test + exam) as total,
((test + exam)/100)*100 as percentage       
FROM school;

暂无
暂无

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

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