[英]How can i get one id result and add it to another in a mysql table?
How can I record all data in a table together?如何将所有数据一起记录在一个表中?
i have values in individual rows that i wish to add up to one.我希望将单个行中的值加起来为一个。 and get the value into a variable.
并将值放入变量中。
what can i use instead of this code below to get all id in the table instead of one?我可以用什么代替下面的代码来获取表中的所有 id 而不是一个?
if(isset($_GET['ide'])){
$coode = $_GET['ide'];
so that once i get all id, i can do the query below...这样一旦我获得了所有 id,我就可以在下面进行查询...
$products = $db->query("SELECT e1,e2 FROM eyfstb WHERE specialnum='$coode'");
while($row = $products->fetch_assoc()){
$e1view = $row["e1"]; $e2view = $row["e2"];
}
and once the query is done, i want to be able to store them in a variable like below查询完成后,我希望能够将它们存储在如下变量中
$final = (e1,e2 of id1) + (e1,e2 of id2) + (e1,e2 of id3) + (e1,e2 of id4);
fine is 5 good is 4 fair is 3 my e1 is fine which is equal 5 my e2 is good which is equal 4很好 5 好 4 公平 3 我的 e1 很好 等于 5 我的 e2 很好 等于 4
making 9 when i added it.当我添加它时制作 9。 but i want to get for all record rows in the table
但我想获取表中的所有记录行
currently i'm able to get the details for only one student from the url $coode but i want to get for all the student from a table and be able to add the resulting data.目前,我只能从 url $coode 获取一名学生的详细信息,但我想从表中获取所有学生的详细信息并能够添加结果数据。
Table Structure表结构
id | e1 | e2 |
---------------------------
1 | fine | good |
2 | good | good |
3 | fair | fine |
Better way is to store in database parameters like this as integer.更好的方法是将这样的数据库参数存储为 integer。 It generates less problems with math operations or comparison.
它在数学运算或比较方面产生的问题更少。
But if you can't refactor it you should map each value to their numerical equivalent.但是,如果你不能重构它,你应该将每个值 map 转换为它们的等效数值。
function mapDbValue(string $value): int {
switch ($value) {
case 'fine': return 5;
case 'good': return 4;
case 'fair': return 3;
}
}
And now in your while
loop map values and add it.现在在您的
while
循环中 map 值并添加它。
$final = 0;
while (...) {
$final += mapDbValue($row['e1']) + mapDbValue($row['e2']);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.