简体   繁体   English

如何获得一个 id 结果并将其添加到 mysql 表中的另一个?

[英]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.

相关问题 如何将一个表的ID值获取到另一个表 - How can i get one table id values to another table 如何将每个结果添加到 MySQL 中的表中 - How can I add each result to my table in MySQL 如何使用PHP或MySQL对一个表中的不同列求和,并在另一表中获得求和结果? - How to sum different columns from one table and get the sum result in another table using PHP or MySQL? 如何在另一个表中执行的mysql查询中添加条件? - how can i add condition in mysql query executed in another table? 当table1.id = table2.id时如何打印一件事,而如果“ if else”又如何打印? - How can I print one thing when table1.id=table2.id, and another thing 'if else'? 如何从phpMyadmin中的一个表中获取ID并将其添加到插入时的另一个表字段中 - How do get a ID from one table in phpMyadmin and add it to another table field on insert 我如何获取id和ip_address并在Codeigniter的ci_session表中添加一列user_id并添加一 - How can I get the id and ip_address and add one more column user_id in ci_session table in Codeigniter and add one 从codignitor中的另一个mysql表获取基于post_id匹配的会话user_id结果 - get session user_id result based on post_id match from another mysql table in codignitor MySQL我可以从一个表还是另一个表中选择数据? - MySQL Can I Select Data from One table OR another Table? 如何获取组头结果另一个表MySQL查询 - How to Get Group Head Result another Table MySQL Query
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM