计算发票中物品的总价

时间:2020-04-23 02:06:09

标签: php sql pdo

这是我所拥有的:-

version: '2'

services:
  orderer-org0:
    container_name: orderer-org0_bh
    image: hyperledger/fabric-orderer:2.0
    environment:
      - ORDERER_HOME=/tmp/hyperledger/org0/orderer
      - ORDERER_GENERAL_LISTENADDRESS=0.0.0.0
      - ORDERER_GENERAL_LISTENPORT=8050
      - ORDERER_GENERAL_GENESISMETHOD=file
      - ORDERER_GENERAL_GENESISFILE=/tmp/hyperledger/org0/orderer/genesis.block
      - ORDERER_GENERAL_LOCALMSPID=org0MSP
      - ORDERER_GENERAL_LOCALMSPDIR=/tmp/hyperledger/org0/orderer/msp
      - ORDERER_GENERAL_TLS_ENABLED=true
      - ORDERER_GENERAL_TLS_CERTIFICATE=/tmp/hyperledger/org0/orderer/tls-msp/signcerts/cert.pem
      - ORDERER_GENERAL_TLS_PRIVATEKEY=/tmp/hyperledger/org0/orderer/tls-msp/keystore/key.pem
      - ORDERER_GENERAL_TLS_ROOTCAS=[/tmp/hyperledger/org0/orderer/tls-msp/tlscacerts/tls-10-1-203-12-8052.pem]
      - ORDERER_GENERAL_LOGLEVEL=debug
      - ORDERER_DEBUG_BROADCASTTRACEDIR=data/logs
    volumes:
      - /home/bhlee/fabric_multi_ca/organizations/org0/orderer:/tmp/hyperledger/org0/orderer/
    ports:
      - 8050:8050

这是购物篮表格,我正在按companyexported(比尔)对每个项目进行排序 所以我想计算账单的总成本(所有相同值(companyexported)行的总和(价格),其中包含相同的公司名称,当然也按accountid排序 我尝试过的: 代码:-

accountid    |    productname     |   qty    |   price       |    companyexported   |
8            |    name            |    5     |    50         |         company1     |
8            |    name2           |    5     |    60         |         company1     |
8            |    name3           |    10    |    10         |         company2     |

1 个答案:

答案 0 :(得分:2)

要获取每个公司的总和,您需要GROUP BY companyexported,而不是ORDER BY。请注意,只要您准备一个语句,就应该对参数使用占位符:

$price = $PDOCon->prepare("SELECT SUM(price) AS totalprice
                           FROM basket
                           WHERE accountid = :accountid
                           GROUP BY companyexported");
$price->bindParam(':accountid', $accountid);
$price->execute();

此外,由于您将获得多个结果(每个公司一个),因此您的取回应该处于循环状态:

while ($row = $price->fetch(PDO::FETCH_ASSOC)) {
    echo $row['totalprice'];
}