//計算訂單數量的總和
<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><<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>
如果輸出的數字後面跟著很多位元的小數,可以在SQL中將該數值的屬性設定為DECIMAL(16,1)。
後面的1是表示保留1位小數點。
以上就介紹了SQL同一個表,PHP分別查詢2列資料的總和然後輸出,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。