Home  >  Article  >  Backend Development  >  In the same SQL table, PHP queries the sum of two columns of data and then outputs it.

In the same SQL table, PHP queries the sum of two columns of data and then outputs it.

WBOY
WBOYOriginal
2016-08-08 09:24:181255browse

//Calculate the sum of the order quantity

<code><span>$query_num</span> = <span>"SELECT SUM(order_num) from hcf_order_db where order_id = XXX"</span>;
<span>$arr_qn</span> = mysqli_query(<span>$dbc</span>,<span>$query_num</span>);
<span>list</span>(<span>$sum</span>)=mysqli_fetch_row(<span>$arr_qn</span>);</code>

//Calculate the sum of the order amount

<code><span>$query_amount</span> = <span>"SELECT SUM(order_amount) from hcf_order_db where order_id = XXX"</span>;
<span>$arr_qm</span> = mysqli_query(<span>$dbc</span>,<span>$query_amount</span>);
<span>list</span>(<span>$sum2</span>)=mysqli_fetch_row(<span>$arr_qm</span>);</code>

Then the PHP output:

<code><span><<span>td</span><span>class</span>=<span>'font-songti'</span><span>align</span>=<span>'center'</span>></span><span><span><?php</span><span>echo</span><span>$sum</span><span>?></span></span><span></<span>td</span>></span><span><<span>td</span><span>class</span>=<span>'font-cali'</span><span>align</span>=<span>'right'</span>></span><span><span><?php</span><span>echo</span><span>$sum2</span><span>?></span></span><span></<span>td</span>></span></code>

If the output number is followed by many decimal places, you can set the attribute of the value in SQL Set to DECIMAL(16,1).
The following 1 means retaining 1 decimal point.

The above introduces the same table in SQL, and PHP queries the sum of two columns of data and outputs it, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn