简体   繁体   English

从 php 中单行的 2 列中获取值

[英]To get value from 2 column in single row in php

could somebody help me, as I just very new in PHP有人可以帮助我吗,因为我在 PHP 中很新

Description :-说明:-

  • In my table, I have 3 column: Adult, Child, Totalpax在我的表中,我有 3 列:成人、儿童、Totalpax
  • I just give the value into Adult & Child column and leave Totalpax column empty我只是将值赋给 Adult & Child 列并将 Totalpax 列留空
  • Then, I try to make a calculation so that Totalpax filed get the amount from Adult + Child column然后,我尝试进行计算,以便提交的 Totalpax 从 Adult + Child 列中获取金额

What have I do :-我做了什么:-

  • Below is my code..下面是我的代码..
  • The problem is - this code give me the total number from my table.问题是 - 这段代码给了我表格中的总数。
  • BUT - I just want to get the value just from singe row..但是 - 我只想从单行中获取价值..
  • Is my code below wrong?我下面的代码错了吗? if so, how to create a code, so that I just get the amount from single row?..如果是这样,如何创建代码,以便我只从单行获取金额?..

Thank in advance..预先感谢..

My Code :-我的代码:-

<?php 
foreach($pdo->query("SELECT SUM(adult+child) FROM test WHERE testID = testID") as $totalpax) {
echo $totalpax['SUM(adult+cwhild)'];}
?>

SUM is an aggregate function which totals all the rows for the given expression. SUM是一个聚合 function ,它总计给定表达式的所有行。

To get a value for each row separately simply add them:要分别获取每一行的值,只需添加它们:

SELECT adult+child as total FROM test

And then接着

echo $totalpax['total']

This is your query:这是您的查询:

SELECT SUM(adult+child)
FROM test
WHERE testID = testID

Start with the WHERE clause.WHERE子句开始。 It is comparing a problem to itself -- so this returns information from all rows where testID is not NULL .它将问题与自身进行比较——因此这将返回testID不是NULL的所有行的信息。

What information does it return?它返回什么信息? That information is the SUM() of two columns.该信息是两列的SUM() This is an aggregation query that returns one row, which has one column which is the sum of adult + child on basically all rows in the table.这是一个返回一行的聚合查询,其中有一列是表中基本上所有行的adult + child的总和。

Then, I try to make a calculation so that Totalpax filed get the amount from Adult + Child column.然后,我尝试进行计算,以便提交的 Totalpax 从“成人+儿童”列中获取金额。

As you have described the problem, you want an update not a select .正如您所描述的问题,您需要update而不是select That would be:那将是:

update test
    set Totalpax = adult + child;

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

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