Rumah > Artikel > pembangunan bahagian belakang > SQL同一个表,PHP分别查询2列数据的总和然后输出
//计算订单数量的总和
<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>
//计算订单金额的总和
<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>
然后就是PHP输出:
<code><span>td</span><span>class</span>=<span>'font-songti'</span><span>align</span>=<span>'center'</span>><span><span><?php </span><span>echo</span><span>$sum</span><span>?></span></span><span><span>td</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></span></code>
如果输出的数后面跟很多位的小数,可以在SQL里将该数值的属性设置为 DECIMAL(16,1)。
后面的1是表示保留1位小数点。
以上就介绍了SQL同一个表,PHP分别查询2列数据的总和然后输出,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。