[英]To get value from 2 column in single row in php
could somebody help me, as I just very new in PHP有人可以帮助我吗,因为我在 PHP 中很新
Description :-说明:-
What have I do :-我做了什么:-
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.