我需要幫助得到一些兩行具有相同的ID: 我知道SQL代碼,這裏面是:獲取在SQL中使用相同的ID行的總和PHP
SELECT orderID,sum(itemPrice) as sum FROM orderitems GROUP BY orderID;
但我不不知道如何在PHP中實現它。 這是我的代碼:
<?php
require('database.php');
$query = 'SELECT * FROM `orderitems`';
$statement = $db->prepare($query);
$statement->execute();
$addresses = $statement->fetchAll();
$statement->closeCursor();
$order_id = filter_input(INPUT_POST, 'order_id');
$total='SELECT SUM(itemPrice)
FROM orderitems
group by orderID';
$statement = $db->prepare($total);
$statement->execute();
$total_order= $statement->fetchAll();
?>
<html>
<head>
<title>Total of the order</title>
<link rel="stylesheet" type="text/css" href="main.css" />
</head>
<body>
<h1>
Total for an order
</h1>
<h4>
<label>orderID : </label> <?php echo $order_id ;?>
</h4>
<p>The total for this order consisting of :</p>
<p><?php echo $total_order; ?>
</p>
</body>
</html>
但它沒有顯示的結果,而不是寫簡單ARRAY
我什var_dumb
但然後它表明這樣的:
陣列(3){[0] => array(2){[「SUM(itemPrice)」] => string(6)「399.00」[0] => string(6)「399.00」} [1] => array(2){[ (itemPrice)「] => string(6)」699.00「[0] => string(6)」699.00「} [2] => array(2){[」SUM(itemPrice)「] => string(7 )「1048.99」[0] => string(7)「1048.99」}}
它顯示正確的答案,但我想擺脫這個其他的東西。
耶酷感謝名單了很多隻是多了一個有利請如何與各個不同的ID得到的只是一個總和 –
http://stackoverflow.com/users/3522312/scaisedge –
更好地解釋你需要onky的第一個結果或或某人其他 – scaisEdge