使用sql查询获取乘法结果的所有2列总和

时间:2018-06-09 09:41:17

标签: mysql

我在MySQL数据库表中有以下列:

noOfChild    noOfAdult    childPrice    adultPrice
==================================================
1            1            120           174  
3            2            14            47
4            3            45            49

现在,我希望获得所有noOfChild * childPricenoOfAdult * adultPrice的总和。

为此,我正在使用此查询,但它只显示第一行结果而不是所有行。我怎么能得到它?

$account->rowQuery("SELECT (noOfChild * childPrice) AS totalChildPrice, 
(noOfAdult * adultPrice) AS totalAdultPrice FROM booking ");

结果应为:

1 X 120 = 120
3 X 14  =  52
4 X 45  = 180
         =====  
          352 for child 

1 X 174 = 174
2 X 47  =  74
3 X 49  = 147
         =====  
          395 for adult 

2 个答案:

答案 0 :(得分:1)

Add the jar file required for jsp. For maven project add following dependency in pom.xml file,
       <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>

答案 1 :(得分:0)

更标准化的设计可能如下所示:

order_detail
order_id category  quantity price
     101 child            1   120
     101 adult            1   174
     102 child            3   14
     102 adult            2   47
     103 child            4   45
     103 adult            3   49

然后查询变得微不足道......

SELECT category, SUM(quanity * price) FROM order_detail GROUP BY category;